1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Custom error handler pipe operator function for RxJS

Discussão em 'Angular' iniciado por Christopher, Junho 25, 2025.

  1. Christopher

    Christopher Guest

    My goal is to create a new custom RxJS pipe operator which can handle every input like this

    constructor(private errorHandler: ErrorHandlerService) {
    const voidStream$ = of() as Observable<void>;
    const stringStream$ = of('');
    const objectStream$ = of({ id: 1, value: 'some value' });

    voidStream$.pipe(handleError(this.errorHandler, () => of(undefined)));
    stringStream$.pipe(
    handleError(this.errorHandler, () => {
    // do some more stuff

    return of('');
    })
    );
    objectStream$.pipe(handleError(this.errorHandler, () => of(undefined)));
    }


    The main issue is how to deal with changing the return type.

    export function handleError<T>(
    errorHandler: ErrorHandlerService,
    innerFunction: (error: any) => Observable<T>,
    ): OperatorFunction<T, T> {
    return (source: Observable<T>): Observable<T> => {
    return source.pipe(
    catchError((error) => {
    errorHandler.handleError(error);

    return innerFunction(error);
    })
    );
    };
    }


    Here is a Stackblitz you can play with.

    Continue reading...

Compartilhe esta Página