I have a situation where I call an API to save data both manually and automatically,
- In the case of manual save I want to show the error if the save fails, and I achieved this using an angular global error handler.
- In the case of automatic save I want to ignore the error, but as I have a global error handler it's catching the error and showing it to the user as per the implementation.
I am trying to find a way to manage this error handling based on some conditions.
following are some code snippets for a better understating of the problem
Global error handler
export class GlobalErrorHandlerService implements ErrorHandler {
constructor(private notification2Service: NotificationService,
private loggingService: LoggingApiService) {
}
handleError(error: any): void {
this.notification2Service.notify(error.message);
this.loggingService.logError(error);
}
}
Save method
post(path: string, body: Object = {}, skipSpinner = 'false'): Observable<any> {
let fullPath = this.getBaseApiUrl(path);
return this.http.post(fullPath,body,{
headers: {'skip-spinner':skipSpinner}
});
}
what I want is global error handler should ignore errors in this call if "skipSpinner" is 'true'
Old question but it might be useful for some people to still get this answered.
You want to implement
HttpInterceptorinstead ofErrorHandler. The first is for HTTP errors the latter for application errors. If theHttpInterceptoris not triggered it will go through toErrorHandlerif the exception is not caught before.Here is one example class that intercepts HTTP errors and filters some based on StatusCode, some on the request URL and some based on headers. Feel free to use whatever you need.