-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(case error): Replaced conditional type with Exclude
* fix(caseError): fixed caseError typings (now works with non-compatible errors and with Tasks errores with only one type) * fix(caseError): improve caseError typings (fix #2) * chore(caseError): removed debugging code
- Loading branch information
Showing
2 changed files
with
28 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
import { Task } from '@ts-task/task' | ||
import { Task, UncaughtError } from '@ts-task/task' | ||
|
||
export type Constructor<T> = { new (...args: any[]): T } | ||
|
||
export type IErrorHandler<E, TResult, EResult> = (err: E) => Task<TResult, EResult> | ||
export type ErrorHandler<ErrorToHandle, TResult, EResult> = (err: ErrorToHandle) => Task<TResult, EResult> | ||
|
||
export function caseError<E, TResult, EResult>( | ||
errorType: Constructor<E>, | ||
errorHandler: IErrorHandler<E, TResult, EResult> | ||
export function caseError<ErrorToHandle, TResult, EResult>( | ||
ErrorType: Constructor<ErrorToHandle>, | ||
errorHandler: ErrorHandler<ErrorToHandle, TResult, EResult> | ||
) { | ||
return function<RE>( | ||
err: RE | ||
): RE extends E ? Task<TResult, EResult> : Task<TResult, RE | EResult> { | ||
return function <InputError> ( | ||
err: InputError | ErrorToHandle | ||
): Task<TResult, EResult | Exclude<InputError, ErrorToHandle>> { | ||
// If the error is of the type we are looking for (E) | ||
if (err instanceof errorType) { | ||
if (err instanceof ErrorType) { | ||
// Transform the error | ||
return errorHandler(err) as any | ||
return errorHandler(err); | ||
} else { | ||
// If not, leave as it is | ||
return Task.reject(err) as any | ||
return Task.reject(err as Exclude<InputError, ErrorToHandle>); | ||
} | ||
} | ||
} |