Supress Error Interceptor

September 24, 2025 · 2 min readLucian Ciobanu
Tags:Angular

As it is often the case, I have to enable an exception to a rule that's working very well - the error handling interceptor should not trigger the toast message when looking for a recent information as the user might not yet have that data assign to it.

In order to achieve this, I first created a "suppres error status" constant and added a rule in the interceptor to go a different logic path once it comes across this.

//shared file
export const SUPPRESS_ERROR_STATUS = new HttpContext<string>(() => '');

//service

import { SUPRESS_ERROR_STATUS } from '../shared';
//other imports

@Injectable({
  providedIn: 'root'
})

export class ThisService {
  getInfo(): Observable<Model | null> {
    const context = new HttpContext.set(SUPPRESS_ERROR_STATUS, '404');
    return this.httpClient.get<Model>(`${this.apiUrl}`, { context })
  }
}

And in the interceptor

export function httpErrorHandlingInterceptor(
  request: HttpRequest<unknown>,
  next: HttpHandlerFn
): Observable<HttpEvent<unknown>> {
  return next(request).pipe(
    catchError((error: HttpErrorResponse) => {
      showHttpError(error, request);
      throw error;
    })
  );
}

function showHttpError(error, request): void {
  try {
    const supressStatus = request.context.get(SUPRESS_ERROR_STATUS);
    if (suppressStatus && supressStatus.includes(status.toString()) {
      return;
    }
    // happy flow continues
  }
}
   

Hope this helped!

Cheers,
Lucian

Picture of author

Web Development & Software Delivery