Last active
September 29, 2020 13:32
-
-
Save leandro-hermes/147ed79da14c0545e59325cd52fedc70 to your computer and use it in GitHub Desktop.
RxJS operator that catches the error, feed an variable with error message, then returns an observable with default value
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 { of as observableOf, OperatorFunction } from 'rxjs'; | |
import { catchError } from 'rxjs/operators'; | |
// tslint:disable-next-line:max-line-length | |
export function handleError<T, O = any>(context: any, attrName: string, defaultMessage?: string, returnedValue?: any): OperatorFunction<T, T | O> { | |
return catchError((err: any) => { | |
context[attrName] = err.error?.message || defaultMessage; | |
return observableOf(returnedValue || null); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment