0

I would like to perform error handling in the use case method using the subscription. If an error is thrown in the adapter, the handling should be performed in the use case. Unfortunately, the catch does not work with the example below, only the error from the adapter is thrown.

  public checkInUsecase(): void {
    this.checkInAdapter().subscribe(
      (data) => {
        this.logger.debug('Work...');
      },
      (error) => {
        this.logger.error('Error.');
      },
      () => {
        this.logger.debug('Successful.');
      }
    );
  }

  public checkInAdapter(): Observable<boolean> {
    throw new Error('Check in error');
  }
1

2 Answers 2

1

Error is thrown inside an observable. In your example the function is not returning an observable.

public checkInUsecase(): void {
    this.checkInAdapter()
        .pipe(catchError(err) => {
            this.logger.error('Error.');
            throw EMPTY;
        })
        .subscribe((data) => {
            this.logger.debug('Work...');
        });
}

public checkInAdapter(): Observable < boolean > { // this method needs to return an observable
    return new Observable((subscriber) => {
        if (!isAllGood) {
            throw Error('error message'); // error is thrown inside the observable.
        }
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

How does it work when the returned observable is from another method and the error is thrown not in the returned observable like below. ` public checkInAdapter(): Observable<boolean> { if(!isAllGood) { throw Error('error message'); } return api.checkin(); } `
if I am understanding you correct, your error is thrown even before you have reached the 'return api.Checkin()', in that case its not an observable error, becase observer has not even been subscribed to yet. you can imagine the error being even thrown even before calling thishis.checkInAdapter()
Do you only know about error after the repsonse or even before subscribing, these two situation will have totally different code.
0

There's a special pipe in Angular that allows you to catch errors in an Observable and react accordingly.

public checkInUsecase(): void {
  this.checkInAdapter()
    .pipe(catchError(err) => {
        this.logger.error('Error.');
        throw EMPTY;
    })
    .subscribe((data) => {
        this.logger.debug('Work...');
    });
}

public checkInAdapter(): Observable<boolean> {
  throw new Error('Check in error');
}

3 Comments

This does not work for me.
does this case make sense when the checkInAdapter will never even return an observable?
@laprof that's because you're not throwing the error correctly. It's not a problem with the catchError pipe. I was just showing you pseudo-code to show the correct way to handle an error.

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.