Angular 4 - Observed catch error

How can I solve the problem of returning with an error using capture inside Observable?

I want to execute a function inside catch in order to do some validation before the subscription is done.

Thank you in advance for your great attention.

The 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): An argument of type '(e: any) => void' is not assigned to type '(err: any, catch: Observable) => ObservableInput <{}> '.
The type 'void' is not assigned to the type 'ObservableInput <{}>'.

+18
source share
4 answers

catch() Observable, Observable.throw(),

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)
  }
}
Hide result

Observable.throw()

+24

.

.catch(e => { console.log(e); return Observable.of(e); })

, :

.catch(e => { console.log(e); return Observable.of(null); }).filter(e => !!e)

, Falsey. , , , , , , , , /.

:

-

.catch(e => Observable.empty())
+15

With angular 6 and rxjs 6 is Observable.throw() Observable.off()deprecated, instead you need to usethrowError

example:

return this.http.get('yoururl')
  .pipe(
    map(response => response.json()),
    catchError((e: any) =>{
      //do your processing here
      return throwError(e);
    }),
  );
+6
source

You should use below

return Observable.throw(error || 'Internal Server error');

Import the statement throwusing the following line

import 'rxjs/add/observable/throw';
+2
source

Source: https://habr.com/ru/post/1684962/


All Articles