Home > OS >  RXJS Custom catcherror operator
RXJS Custom catcherror operator

Time:12-10

Normally I would be able to define a custom operator and structure a pipe like this:

public custOperator = () => {
    return (source: Observable<any>) => {
        return source.pipe(
            map(val => {
                //do stuff with val
            })
        )
    }
};

test(){

    of().pipe(
        this.custOperator(),
        catchError(err => {
            //do stuff with err (more logic will go in here)
            throw err
        })
    ).subscribe()
}

However it's getting tedious writing the catcherror logic every time I need to pipe an observable.

How would I go about creating a similar operator except that it expects an error instead of an observable, such that it mimics catchError?

Update

I was able to create a custom catcherror operator as such:

private customCatchOperator = () => {
    return catchError(err => {
        // handle error 
    })
}

CodePudding user response:

You can do it as follows:

customCatchOperator() {
   return catchError(err => {
            // handle error
        })
}
  • Related