1

I use RxJS and I need to return an observable of that type im my method: Observable<MyStuff>

When I want to throw an error I use:

import { throwError } from "rxjs";
/// ... stuff
return throwError('Error stuff invalid');

throwError return an Observable<never> and it's not the same type as Observable<MyStuff>

I want to throw an error with that type: Observable<MyStuff> I don't want to change the return type of my function if it's possible

What is the best practice to handle error with observables ?

4
  • Where you have an Observable with type Observable<MyStuff>? Commented Nov 30, 2018 at 9:24
  • This is my return type Commented Nov 30, 2018 at 9:26
  • When everything goes well I return that type Commented Nov 30, 2018 at 9:26
  • Can you show the whole chain? Commented Nov 30, 2018 at 9:32

1 Answer 1

2

You can throw an error without necessarily using ThrowError from rxjs , you can do it in this way , you replace return ThrowError("Your error"); with throw "Error";

and simply catch it when subscribing to your observable like follow YourrReturnedObservable.subscribe(successData => {Your code logic } , err => { // catch exceptions here} );

Sign up to request clarification or add additional context in comments.

1 Comment

Just perfect, yeah so now I think when you are on a pipe you have to just throw and It will go to the error handling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.