dimanche 4 novembre 2018

How to disable error function on status 401

Hello i was trying to use an 401 (Unauthorized) and 403 (forbidden) httprequests interceptor, and i've had a problem with disabling the error function, on a 401 error.

The interceptor:

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

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(req).do(event => {}, err => {
            if (err instanceof HttpErrorResponse && err.status == 401) {
                // alert unauthorized & redirect to login.
            }
        });
    }
}

The request:

request(data){
    this.httpservice.request(data).subscribe(
      res => alert("success"),
      err => {
          /* doing something very complex that unauthorized
          users should not process to */
      }
    );
}

So in the given example both the interceptor code (the redirection) and the request err handling function are running. I want to disable the err result function in case there is a 401 result from the server. Unfortunately i could not find a way to do it.




Aucun commentaire:

Enregistrer un commentaire