Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform a HttpResponse into a HttpErrorResponse

Tags:

angular

I have a web service returning custom errors inside a 200 Response, in a classic errors object.

This breaks my workflow and patterns, where I usually intercept proper HTTP error status codes and go on with handling the various error cases from there.

I need to transform these custom errors into proper HttpErrorResponse errors via an interceptor, so that I can then intercept those now-proper errors and continue with my application per usual best practices.

Things I can't do:

  • Have the web service modified so that it returns proper HTTP errors
  • Have another web service in the middle to handle the transformation before the Response hits the client application

What I'd like to do:

  • Make the following Stackblitz example work. The part that needs to be fixed is in interceptors/custom-error.interceptor.ts
    https://stackblitz.com/edit/angular-pnv63p

So that when in the near future I'll be working on several other applications that use the same unmodifiable web service, I can just drop the CustomErrorInterceptor in and call it a day.

like image 991
William Ghelfi Avatar asked Jul 20 '26 01:07

William Ghelfi


1 Answers

And here's the answer a buddy of mine just found:

throw new HttpErrorResponse({ error: 'bar', status: 403 });

like image 125
William Ghelfi Avatar answered Jul 21 '26 16:07

William Ghelfi