Last active
January 15, 2021 19:13
-
-
Save anthonyjoeseph/0cdb5ef29c8622648cd022d761b88e2c to your computer and use it in GitHub Desktop.
Catch Error Experiment
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
import { flow, pipe } from 'fp-ts/function'; | |
import * as E from 'fp-ts/Either' | |
import * as r from 'rxjs' | |
import * as ro from 'rxjs/operators' | |
import * as OB from 'fp-ts-rxjs/lib/Observable' | |
export const tryCatch = <E, A>( | |
onErr: (e: unknown) => E | |
): r.OperatorFunction<A, E.Either<E, A>> => flow( | |
OB.map(E.right), | |
ro.catchError(flow(onErr, E.left, OB.of)) | |
); | |
const exceptionOnError = (a: unknown) => { | |
throw new Error('new error') | |
} | |
const a = pipe( | |
r.throwError(new Error('original error')), | |
tryCatch(exceptionOnError), | |
) | |
a.subscribe((e) => { | |
console.log(JSON.stringify(e)) | |
}, (error) => { | |
console.log(`caught by observable: ${(error as Error).message}`) | |
}) | |
// output: | |
// caught by observable: new error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment