javascriptangularerror-handlingangular-http-interceptorsangular-errorhandler

Angular interceptor error handling put & delete method problem


I show an alert based on the http response returned in Angular. My code works in Get and post methods, but not in put and delete methods.I'm stuck here and would be very happy if someone could help me. ApiUrl.ts => export const AlertUrls: string[] = [ // backend service urls ]

my-interceptor.ts => import { AlertUrls, collection } from "../ApiUrl";

  return next.handle(req).pipe(
    map((response: any) => {
        if ( AlertUrls.includes(req.url) && req.method !=='GET' && response instanceof HttpResponse && response.status === 200) {
          this.alertService.showSuccessAlert();
        }
    return response;
  }),

Solution

  • Url not matching is the problem you are facing, you can simply use the string property, includes to check if the string is present inside the url, instead of doing a full equality check!

    AlertUrl: /delete

    will match for

    Full URL: /delete/123uuh4u23424

      return next.handle(req).pipe(
        map((response: any) => {
            if (AlertUrls.find((url: any) => req.url.includes(url)) && req.method !=='GET' && response instanceof HttpResponse && response.status === 200) {
              this.alertService.showSuccessAlert();
            }
        return response;
      }),