RxJS how to return Observable with default error handling function

Francesco Borzi

In my application I have some methods returning an Observable:

public myMethodReturningObs(...): Observable {
  return this.http.get(...); // this will return an Observable
}

then I call this method in several places around my application:

this.service.myMethodReturningObs().subscribe(
 (data) => { /* do something with data */ },
 (error) => { console.log('Error!'); }
);

// ...

this.service.myMethodReturningObs().subscribe(
 (data) => { /* do something else with data */ },
 (error) => { console.log('Error!'); }
);

I was wondering if there is a way inside myMethodReturningObs(...) to attach the a default error handling function, which in my example would be console.log('Error!');, so I don't have to repeat it every time I subscribe to the Observable returned by myMethodReturningObs(...).

Ideally I need something like:

public myMethodReturningObs(...): Observable {
  return this.http.get(...)
   .onError({ console.log('Error!'); }); // this will return an Observable
}

so then I can just do:

this.service.myMethodReturningObs().subscribe(
 (data) => { /* do something with data */ }
);

// ...

this.service.myMethodReturningObs().subscribe(
 (data) => { /* do something else with data */ }
);
udalmik

You can catch error in base observable, log it and propogate further

public myMethodReturningObs(...): Observable {
  return this.http.get(...)
        .catch(e => {
          // log error
          console.error('Failed', e);
          // return same error, so subscriptions also fail
          return Rx.Observable.throw(e)
        });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to handle error and return observable while subscribe inside an observable function in Rxjs Angular

How to return default error if observable next is undefined

Error rxjs_Observable__.Observable.forkJoin is not a function?

Angular 12/Typescript/rxjs: error handling of nested Promises and rxjs Observable

Angular 12/rxjs/observables: function with chained subscriptions: return statement and error handling

How to return stubbed Observable<void> in RxJS

How do you return new Observable(function(observer) { ... with RxJS v5?

how to wait for an observable but return the previous observable result in rxjs?

How to use observable and pipes in a function rxjs?

How to return a Observable in promise function

How to call both next and error on RxJS Observable

How to test component error handling with observable service

How to subscribe multiple observable in rxjs 6 ? do is not a function in rxjs 6

Observable error while trying to return function result

Error constructing an rxjs/Observable

Handling Error Thrown by Observable

Angular Observable error handling

error handling in observable or in observer?

Handling error in Observable

rxjs: how to return the result from another observable from catchError

How to return observable with RxJS in proper format with additional separate paging data

RxJS clarification needed: how to return a plain object instead of an Observable

How to handle an observable that may return empty when using switchMap RxJS

How to get an Observable to return an array of transformed items (Rxjs)

How to connect two observable and return true if they completes in Angular using RxJs?

How to disable Spring boots default error handling?

How to bypass AEM default error handling?

Angular - RxJS - Observable and method return

RxJs return Observable if variable not undefined