Map Angular HttpClient error with its type

54 views Asked by At

I want to map HttpClient's error with its type and I have this code:

export class ErrorMessageInterceptor implements HttpInterceptor {
  constructor(private notifyService: NotifyService) {}

  public intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).pipe(
      catchError(error => {
        console.error(error);
        return throwError({
          ok: error.ok,
          status: error.status,
          statusText: error.statusText,
          code: error.error.error.code,
          message: error.error.error.message,
          headers: error.headers,
          url: error.url,
        } as OdataError);
      }),
      tap(() => {}, async error => {
        await this.notifyService.error(error.message);
      }),
    );
  }
}

But unfortunately the default type for error in tap operator remains any and I'd not want to map the error type wherever it'll be used. So I have 2 questions here:

  1. Why the mapped type error is not visible in the code above?
  2. Is there a way to set custom error type globally or at least for injected HttpClient's instance?
0

There are 0 answers