How can I solve the problem of return with error, use the capture inside the Observable?
I want to execute a function inside the catch, to do some validation before the subscribe is executed.
Thank you in advance, thank you very much for your attention.
Error occurs in -> .catch( (e) => {console.log(e)} )
import { Injectable } from '@angular/core'; import { Headers, Http, ResponseOptions} from '@angular/http'; import { AuthHttp } from 'angular2-jwt'; import { MEAT_API } from '../app.api'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; @Injectable() export class CompareNfeService { constructor(private http: AuthHttp) { } envirArquivos(order): Observable<any> { const headers = new Headers(); return this.http.post(`${MEAT_API}compare/arquivo`, order, new ResponseOptions({headers: headers})) .map(response => response.json()) .catch( (e) => {console.log(e)} ); } }
Error
ERROR in /XXXXXX/application/src/app/compare/service.ts (28,17): Argument of type '(e: any) => void' is not assignable to parameter of type '(err: any, caught: Observable) => ObservableInput<{}>'.
Type 'void' is not assignable to type 'ObservableInput<{}>'.
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.
The Observable Contract and Error Handling Network requests can fail, for example. A stream can also complete, which means that: the stream has ended its lifecycle without any error. after completion, the stream will not emit any further values.
You'll need to use retry or retryWhen (names are pretty self-explanatory) — these operators will retry a failed subscription (resubscribe to the source observable, once an error is emitted.
The retryWhen operator allows us to do just that. retryWhen takes an observable of errors, and you can return that sequence with an additional delay to space-out the retries.
If you want to use the catch()
of the Observable
you need to use Observable.throw()
method before delegating the error response to a method
import { Injectable } from '@angular/core'; import { Headers, Http, ResponseOptions} from '@angular/http'; import { AuthHttp } from 'angular2-jwt'; import { MEAT_API } from '../app.api'; import { Observable } from 'rxjs/Observable'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; @Injectable() export class CompareNfeService { constructor(private http: AuthHttp) {} envirArquivos(order): Observable < any > { const headers = new Headers(); return this.http.post(`${MEAT_API}compare/arquivo`, order, new ResponseOptions({ headers: headers })) .map(response => response.json()) .catch((e: any) => Observable.throw(this.errorHandler(e))); } errorHandler(error: any): void { console.log(error) } }
Using Observable.throw()
worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With