Catch every request error in Angular

HTTP interceptor

Yurii K
Nerd For Tech
2 min readJan 12, 2022

--

The simple request in angular looks like this. You inject an HTTP client, make a subscription of GET or POST request, and of course, you make an error handler.

A simple project could have from 50–200 request subscriptions. Imagine now you need to make logging of every error in request. How you can do that?

Well, you can put a new logging logic for every error handler. This plan will take a lot of time but it will work. The problem with that plan when you make a new request subscription you need to put a logging logic in the error handler. You could forget to do that.

Instead, we can make an interceptor and we will analyze every response, by checking its status code.

This interceptor we need to add in the main module in the provider section.

From now on all our requests will go through this interceptor.

If you need to take a close look at the project here is the link.

Originally published at http://tomorrowmeannever.wordpress.com on January 12, 2022.

--

--

Yurii K
Nerd For Tech

Full-stack developer. In my work, I use Angular and C#. In my free time I write articles for my blog.