Sub topic 15
Common pitfalls and limits
This chapter assumes you can write and register a functional interceptor with withInterceptors, that you know clone(), and that you are comfortable with pipe, tap and catchError. It collects the mistakes that cost the most debugging time, and the places where interceptors do not apply.
Forgetting to call next
An interceptor must return next(req), or an observable built from it. If it returns EMPTY or never calls next, the request is never sent. The caller's observable completes or hangs with no error, so the cause is hard to see. Early returns are the usual source: every branch must either forward the request or fail deliberately.
Cloning is not optional
HttpRequest is immutable, so req.headers.set(...) returns a new HttpHeaders object and leaves the request unchanged. Calling it and then passing req to next sends the original request without your header. Always forward the result of req.clone(...).
Interceptor loops
An interceptor that calls HttpClient sends that call through the same chain. A token refresh is the classic case. The refresh request triggers the interceptor, which starts another refresh, and so on.
import { HttpClient, HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { switchMap } from 'rxjs';
export const refreshInterceptor: HttpInterceptorFn = (req, next) => {
if (req.url.endsWith('/api/refresh')) {
return next(req); // break the loop for the refresh call itself
}
const http = inject(HttpClient);
return http.post('/api/refresh', {}).pipe(switchMap(() => next(req)));
};The guard is the fix. A SKIP_AUTH-style HttpContextToken works too. Injecting HttpBackend and calling it directly also avoids the chain, but it skips every interceptor.
inject() only works synchronously
inject() needs an injection context. Angular provides one while it runs the interceptor function body, but not inside later callbacks such as tap, catchError or setTimeout. Call inject() at the top of the function and keep the result in a variable.
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AuthService } from './auth.service';
export const scopedAuthInterceptor: HttpInterceptorFn = (req, next) => {
const auth = inject(AuthService); // top of the function, not in a callback
if (!req.url.startsWith('/api/')) {
return next(req); // do not send the token to third-party hosts
}
const token = auth.getToken();
return next(token ? req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }) : req);
};The startsWith('/api/') check also prevents a security problem. An unscoped auth interceptor attaches your token to every request, including calls to other domains.
Retry and error handling traps
retryresubscribes tonext(req), so it resends the request. Retrying a non-idempotentPOSTcan create duplicate records. Restrict retries toGETor to specific status codes.- A
catchErrorthat returnsof(...)orEMPTYhides the failure from the caller. Rethrow withthrowError(() => error)unless you intend to recover. - Interceptors see
HttpErrorResponseonly for failed requests. Business errors inside a200body are not errors to the chain.
Limits: what interceptors cannot see
Interceptors run only for requests made through HttpClient. These bypass them:
| Request source | Intercepted? |
|---|---|
| `HttpClient` calls | Yes |
| `fetch()` or `XMLHttpRequest` called directly | No |
| Requests sent through an injected `HttpBackend` | No |
| Images, scripts and stylesheets loaded by the browser | No |
| Third-party SDKs with their own HTTP layer | Usually no |
Exercise
A teammate's interceptor calls inject(Router) inside a catchError callback and throws an error about the injection context. What is the fix?
Show answer
Call inject(Router) at the top of the interceptor function, store it in a constant, and use that constant inside catchError. Angular provides the injection context only while the function body runs synchronously.