1

I need your help to recommend me documents or links that solve my question. Actually in Angular 4, I want to customize error from api services. For example, if the server responses the request with error, the error will be shown to user. My question is how can I get all errors from server and for each ones write codes for user in front-end. Thank you in advance.

Amir Gh
  • 265
  • 1
  • 5
  • 20
  • [link](https://stackoverflow.com/questions/46019771/catching-errors-in-angular-httpclient) this could be helpful, I recommend that you should try to add specific error code in your backend (e.g spring `@ExceptionHandler` ) and in your front end You can try to use `.catch((err: any) => { if(err.status == 'your code e.g 84573'){ console.error('An special error occurred coneccted to the business logic:', err.error); } });` This(more or less this) was working in angularjs - probably will work here too but i'm not 100% sure. – Macryo Feb 27 '18 at 06:49
  • 1
    Possible duplicate of [Catching errors in Angular HttpClient](https://stackoverflow.com/questions/46019771/catching-errors-in-angular-httpclient) – zgue Feb 27 '18 at 07:20
  • @MacRyze thank you, it is so easy :) – Amir Gh Feb 27 '18 at 07:24
  • @zgue thanks a lot, it is useful. – Amir Gh Feb 27 '18 at 07:25

1 Answers1

1

You can make HTTP interceptor and collect all server error in single service and modify them according to your requirement.

This is one of the simplest implementation I have found https://hackernoon.com/global-http-request-error-catching-in-angular-486a319f59ab

Suhel
  • 927
  • 8
  • 19