interceptor error handling angular 9

import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
export class HttpErrorInterceptor implements HttpInterceptor {
  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request)
      .pipe(
        catchError((error: HttpErrorResponse) => {
          let errorMsg = '';
          if (error.error instanceof ErrorEvent) {
            console.log('this is client side error');
            errorMsg = `Error: ${error.error.message}`;
          }
          else {
            console.log('this is server side error');
            errorMsg = `Error Code: ${error.status},  Message: ${error.message}`;
          }
          console.log(errorMsg);
          return throwError(errorMsg);
        })
      )
  }
}

3.44
9
Awgiedawgie 440220 points

                                    // src/app/auth/jwt.interceptor.ts

// ...
import 'rxjs/add/operator/do';

export class JwtInterceptor implements HttpInterceptor {

  constructor(public auth: AuthService) {}

  intercept(request: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
    
    return next.handle(req).do((event: HttpEvent&lt;any&gt;) =&gt; {
      if (event instanceof HttpResponse) {
        // do stuff with response if you want
      }
    }, (err: any) =&gt; {
      if (err instanceof HttpErrorResponse {
        if (err.status === 401) {
          // redirect to the login route
          // or show a modal
        }
      }
    });
  }
}

3.44 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
angular error handler interceptor handling failed request in interceptor angular error handling with http interceptor error handling in angular interceptor angular interceptor get error interceptor error angular alert error interceptor angular error handling with http interceptor angular create an error and interceptor interceptor status code angular how to handle response in angular 8 interceptor angular http interceptor return error how to throw error in interceptor angular angular interceptor catch http error and navigate angular http interceptor for error handling angular show error from interceptor angular http error interceptors examples http interceptor angular error handling angular interceptor handle error interceptor angular capture error angular http interceptor error handling Error from error interceptor angular angular interceptor handle 400 error interceptor error handling angular http error handler and interceptor in angular error in interceptor error handling status code interceptor angular error handling interceptor angular angular interceptor return error handle error interceptors.response http interceptor in angular catach error code interceptor in angular 11 handle error error handler vs error interceptor in angular http interceptor for error handling http interceptor angular 4 error handling angular error interceptor many handle http errors in angular interceptor angular interceptor status code angular error interceptor not catching errors angular 11 http interceptor http error INSIDE INTERCEPTOR ERROR catch http errors angular interceptor angular http error interceptor angular catch angular http error interceptor angular interceptor catch error angular 8 interceptor for status angularjs angular interceptor block code problem AngularJS HTTP interceptor error handling error interceptor angular example angular 11 http error interceptor angular http error handling interceptor Angular 8 http interceptor error handling based on error codes hot to make error interceptor based on rror code in angular medium how to use interceptor for error handling angular interceptor error http interceptor angular 8 catch error error interceptor angular 11 angular interceptor subscribe error catch error using interceptor http interceptor return backend error angular handle error in interceptor in angular how to use error interceptors in angular global error interceptor angular handle error in interceptor angular angular error handling interceptor angular 2 error interceptor angular http error interceptor http error interceptor angular 11 angular http interceptor handle response interceptor angular catcherror angular 11 interceptor request error code handle error in interceptor angular 10 handle error in interceptor angular 6 http interceptor angular catch error angular catch all http error interceptor angular error handler vs interceptor angular interceptor to log error handling how to catchError interceptor in angular throw error from interceptor angular http angular throw error in interceptor angular 8 http interceptor how to return error http interceptor angular 8 error status interceptor vs catch angular error interceptor angular angular 11 interceptor error angular http interceptor types of errors angular error interceptor interceptor handle error suppress angular how to show error in error interceptor in angular 8 angular 10 interceptor error handling angular interceptor error handling interceptor http error handler angular interceptor in angular 8 in details for beginners how to generate interceptor in angular 8 generate error interceptor angular 10 error interceptor angular 10 multiple interceptors in angular http interceptor in angular library angular multiple interceptors how to skip interceptor for get in angular how to skip interceptor for http get in angular angular 5create an http interceptor error interceptor angular 9 interceptor which service threw error angular angular interceptor Understanding HTTP Interceptors in Angular in detail Understanding HTTP Interceptors in Angular in details how to make interceptor in angular 8 with professional example http interceptor service error: 0 angular 9 http interceptor service error angular 9 error handling with http interceptor not working in Angular interceptor angular how to make interceptor request and response in angular 8 intercept error http client angular error interceptor in angular 8 handle response in interceptor to page handle response in interceptor http interceptor in angular 9 http get error handling angular http response interceptor angular 8 http error interceptor angular error handling interceptor angular 8 error handling example angular 8 http error interceptor angular 8 http interceptor gets the error handling angular 8 http inteceptor gets the error interceptor catch error angular 8 real app interceptor method catch error Angular 8 http interceptor error handling angular interceptor catch error best practic to handle code error angular 8 http.error.interceptor angular 10 http interceptor example error interceptor angular 8 http error interceptor how to make interceotir forget latest request that got error interceptor error handling handling http error in interceptor angular how to stop interceptor when back throw browser in angular http interceptor angular 8 error code handling interceptor error http angular 9 angular 9 http interceptor error handling interceptor error handling angular 9
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source