Skip to content

Instantly share code, notes, and snippets.

@leandro-hermes
Last active September 29, 2020 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leandro-hermes/147ed79da14c0545e59325cd52fedc70 to your computer and use it in GitHub Desktop.
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
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