diff --git a/src/task.ts b/src/task.ts index 0e788bc..9a75048 100644 --- a/src/task.ts +++ b/src/task.ts @@ -2,7 +2,7 @@ import { catchError, chain, IMapFn, IPipeFn, ITaskChainFn, map } from './operato export class UncaughtError extends Error { - type: 'UncaughtError'; + type: 'UncaughtError' = 'UncaughtError'; constructor (private error: any) { super('UncaughtError: ' + error.toString()); diff --git a/src/utils/case-error.ts b/src/utils/case-error.ts index 222da51..fb3925d 100644 --- a/src/utils/case-error.ts +++ b/src/utils/case-error.ts @@ -3,12 +3,16 @@ import { Task } from '../task'; export type Constructor = { new(...args: any[]): T; }; export type IErrorHandler = (err: E) => Task; + export function caseError (errorType: Constructor, errorHandler: IErrorHandler) { return function (err: RE | E): Task { + // If the error is of the type we are looking for (E) if (err instanceof errorType) { - return errorHandler(err); + // Transform the error + return errorHandler(err as E); } else { - return Task.reject(err); + // If not, leave as it is + return Task.reject(err as RE); } }; -} \ No newline at end of file +}