Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 - Checking for server errors from subscribe

I feel like this scenario should be in the Angular 2 docs, but I can't find it anywhere.

Here's the scenario

  1. submit a form (create object) that is invalid on the server
  2. server returns a 400 bad request with errors I display on the form
  3. after the subscribe comes back, I want to check an error variable or something (ie. if no errors > then route to newly created detail page)

I imagine it working something like this:

this.projectService.create(project)     .subscribe(         result => console.log(result),         error => {             this.errors = error         }     );  }  if (!this.errors) {     //route to new page } 

I'm very new to Angular 2 so this may come from my lack of understanding in how an Observable works. I have no issue with displaying that data on the form, but can't figure out how to see it within the ts component. I really just want to check the success/fail of the http create.

like image 793
Stephanie Avatar asked Feb 08 '17 04:02

Stephanie


People also ask

How do you handle errors in RxJS subscription?

We need to create the Notification Observable directly in the function passed to the retryWhen operator. This function takes as input argument an Errors Observable, that emits as values the errors of the input Observable. So by subscribing to this Errors Observable, we know exactly when an error occurs.

What does subscribe () do in Angular?

Subscribe() is a method in Angular that connects the observer to observable events. Whenever any change is made in these observable, a code is executed and observes the results or changes using the subscribe method.

How do you catch an observable error?

Catch errors in the observable stream Another option to catch errors is to use the CatchError Operator. The CatchError Operators catches the error in the observable stream as and when the error happens. This allows us to retry the failed observable or use a replacement observable.

How do you handle errors in forkJoin?

forkJoin is an operator that is best for use. If you are having issues in handling multiple requests on page load or want to move to action when all the responses are received. You can easily group the observables and emit the final value for each error.


1 Answers

As stated in the relevant RxJS documentation, the .subscribe() method can take a third argument that is called on completion if there are no errors.

For reference:

  1. [onNext] (Function): Function to invoke for each element in the observable sequence.
  2. [onError] (Function): Function to invoke upon exceptional termination of the observable sequence.
  3. [onCompleted] (Function): Function to invoke upon graceful termination of the observable sequence.

Therefore you can handle your routing logic in the onCompleted callback since it will be called upon graceful termination (which implies that there won't be any errors when it is called).

this.httpService.makeRequest()     .subscribe(       result => {         // Handle result         console.log(result)       },       error => {         this.errors = error;       },       () => {         // 'onCompleted' callback.         // No errors, route to new page here       }     ); 

As a side note, there is also a .finally() method which is called on completion regardless of the success/failure of the call. This may be helpful in scenarios where you always want to execute certain logic after an HTTP request regardless of the result (i.e., for logging purposes or for some UI interaction such as showing a modal).

Rx.Observable.prototype.finally(action)

Invokes a specified action after the source observable sequence terminates gracefully or exceptionally.

For instance, here is a basic example:

import { Observable } from 'rxjs/Rx'; import 'rxjs/add/operator/finally';  // ...  this.httpService.getRequest()     .finally(() => {       // Execute after graceful or exceptionally termination       console.log('Handle logging logic...');     })     .subscribe (       result => {         // Handle result         console.log(result)       },       error => {         this.errors = error;       },       () => {         // No errors, route to new page       }     ); 
like image 178
Josh Crozier Avatar answered Oct 26 '22 22:10

Josh Crozier