-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add catchResult<TValue, TError>() (#192)
- Loading branch information
1 parent
ecf09b8
commit c695fcd
Showing
3 changed files
with
70 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { catchResult } from './try-catch-to-result' | ||
|
||
describe(catchResult.name, () => { | ||
it('should try', done => { | ||
function someThrowable(): string { | ||
return 'I worked!' | ||
} | ||
|
||
const sut = catchResult(someThrowable) | ||
|
||
expect(sut.isOk()).toEqual(true) | ||
expect(sut.unwrap()).toEqual('I worked!') | ||
|
||
done() | ||
}) | ||
|
||
it('should catch', done => { | ||
function someThrowable(): string { | ||
throw new Error('I failed!') | ||
} | ||
|
||
const sut = catchResult(someThrowable) | ||
|
||
expect(sut.isFail()).toEqual(true) | ||
|
||
done() | ||
}) | ||
|
||
it('should catch with error mapping function', done => { | ||
function someThrowable(): string { | ||
throw new Error('I failed!') | ||
} | ||
|
||
class CustomError extends Error { | ||
static fromUnknown(err: unknown): CustomError { | ||
if (err instanceof Error) { | ||
return new CustomError(err.message) | ||
} | ||
return new CustomError('new error') | ||
} | ||
constructor(message?: string) { | ||
super(message) | ||
} | ||
} | ||
|
||
const sut = catchResult(someThrowable, CustomError.fromUnknown) | ||
|
||
expect(sut.isFail()).toEqual(true) | ||
expect(sut.unwrapFail().message).toEqual('I failed!') | ||
|
||
done() | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { fail, ok } from '../result.factory' | ||
import { IResult } from '../result.interface' | ||
|
||
/** | ||
* Ingest a try-catch throwable function so that it doesn't halt the program but instead | ||
* returns an IResult | ||
* @param fn a throwable function | ||
* @returns an IResult object which wraps the execution as either fail or success | ||
*/ | ||
export function catchResult<TValue, TError>(fn: () => TValue, errFn?: (err: unknown) => TError): IResult<TValue, TError> { | ||
try { | ||
return ok<TValue, TError>(fn()) | ||
} catch(err) { | ||
return fail<TValue, TError>(errFn ? errFn(err) : err as TError) | ||
} | ||
} |