0

I am developing a project in Angular 10. I have created a Http Interceptor for handle http error. I have used "map" and "of" operator. But it showing the below error:

Property 'map' does not exist on type 'Observable<HttpEvent>'.
14 .map(resp => {

Property 'of' does not exist on type 'typeof Observable'. 31 Observable.of(err);

Here is my Interceptor code:

import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class HttpErrorHandlerServiceService implements HttpInterceptor {

  constructor() { }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req)
      .map(resp => {
        if (resp instanceof HttpResponse) {
          return resp;
        }
      })
      .catch(err => {
        console.log(err);
        
        if (err instanceof HttpErrorResponse) {
          console.log(err.status);
          console.log(err.statusText);
          
          if (err.status === 401) {
            // redirect the user to login page
            // 401 unauthorised user
          }          
        }
        return Observable.of(err);
      });
  }
}

Can anyone suggest what is the wrong in my code.

2 Answers 2

1

there's no more map or catch operators in the Observable object

use pipe instead :

import { catchError, map } from 'rxjs/operators';

//...
return next.handle(req)
    .pipe(
      map(resp => {
        //....
      }),
      catchError(err => {
        //...
      })
    );
Sign up to request clarification or add additional context in comments.

3 Comments

Argument of type '(err: any) => void' is not assignable to parameter of type '(err: any, caught: Observable<HttpEvent<any>>) => ObservableInput<any>'. Type 'void' is not assignable to type 'ObservableInput<any>'. 21 catchError(err => {
please see the above error. I am facing in catchError operator
I'm not sure what you want to do, you can just return a message like: catchError(_ => of('Error happened !')). Or update your code in the question to see your new implementation
1

I didn't read your entire code but judging from you error you can fix it by using of operator:

instead of return Observable.of(err); do return of(err); and add an import `import { Observable, of } from 'rxjs';

Edit

as MikeOne commented you need also to use the map operator:

import { catchError, map } from 'rxjs/operators';
...
return next.handle(req)
    .pipe(
      map(resp => { ... }),
      catchError(err => { ... })
    );

`

2 Comments

Plus, the .map should be .pipe(map...)
@Rachid O, I tried with import statement. return of(err) instead of Observable.of(err) is working. but map operator still showing error

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.