Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RXJS 6: new version of HttpInterceptor

I am in the process of adding rxjs_compat to my project in order to move to v6 of libraries.

However the existing HttpInterceptor for global error handling no longer compiles. Not sure where to go with it. Tried all sorts. Getting type mismatches with everything tried.

import { Injectable } from "@angular/core";
import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpRequest,
  HttpResponse,
  HttpErrorResponse
} from "@angular/common/http";
import { Observable, of, empty } from "rxjs";
import { ToastrService } from "ngx-toastr";
import { environment } from "../../environments/environment";
import { catchError, map } from "rxjs/operators";

@Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {
  constructor(private toastr: ToastrService) {}
  intercept(
    request: HttpRequest<any>,
    next: HttpHandler
  ): Observable<HttpEvent<any>> {
    return next.handle(request).pipe(
      catchError(err => of(HttpErrorResponse)),
      map(err => {
        let message: string;
         this.toastr.error(`${message}`, "Application Error");
        return Observable.empty<HttpEvent<any>>();
      })
    );
  }
}

src/app/shared/http-error-interceptor.ts(26,27): error TS2339: Property 'empty' does not exist on type 'typeof Observable'.

empty is now a constant, but doesn't accept a type, so that does not work either. Also could not find much in the upgrade notes

EDIT

although interestingly this compiles:

return Observable.of<HttpEvent<any>>();
like image 959
jenson-button-event Avatar asked May 22 '18 19:05

jenson-button-event


1 Answers

  1. import {EMPTY} from 'rxjs';

  2. Replace return Observable.empty() with

    return EMPTY;

like image 131
Yakov Fain Avatar answered Oct 08 '22 12:10

Yakov Fain