Is it possible to bypass the Error Interceptor selectively?

948 views
Skip to first unread message

Partha Majumdar

unread,
Oct 10, 2018, 12:27:55 PM10/10/18
to Angular and AngularJS discussion

Dear Sir/Madam,


I have implemented the error interceptor. It works fine.


Now, I have a situation where I call the same node.js service in 2 places in my application - while I add a new record and when I upload bulk data.


When I add record, if HTTP error is detected, it should display the display. But when I am uploading data, the dialog should not be display. I will handle the error separately.

Is it possible?


Regards,

Partha


import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';
import { Injectable } from '@angular/core';

import { GeneralService } from './general.services';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private generalService: GeneralService) {}

intercept(req: HttpRequest<any>, next: HttpHandler) {
const authToken = this.generalService.getToken();
const authRequest = req.clone({
headers: req.headers.set('Authorisation', 'Bearer ' + authToken)
});
return next.handle(authRequest);
}
}

Sander Elias

unread,
Oct 11, 2018, 12:31:42 AM10/11/18
to Angular and AngularJS discussion

Hi Partha,

Yes, that is possible. The easiest way is adding (and inspect) a flag to your request. I would add a custom header to my requests, that way I can inspect it server side too.

 http.post(someUrl,someData,{header:{"se-error-handling":"none"}})

inspect that in your error interceptor, and don’t handle the error when the header is present.

Regards
Sander

Zlatko Đurić

unread,
Oct 11, 2018, 5:21:14 AM10/11/18
to Angular and AngularJS discussion
Hi Partha,

In addition to what Sander has said, you can be more specific in your error interceptor on the Angular side. Something like this:

intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request) .catch(response => { if (!response instanceof HttpErrorResponse) { // network error, ignore or whatevs return throwError(response); } // we got an error from backend. Options: // you can check e.g. request method if (response instanceof HttpErrorResponse && request.method === 'PUT') { // show or not show the dialog .. // alternatively check by your calling URL } else if (request.url.contains('bulk-upload-endpoint') { // special handling here } Or if it is just one endpoint that needs special handling, catch error there on the call site and swallow it. Hope that gives you ideas.


Partha Majumdar

unread,
Oct 13, 2018, 5:38:49 AM10/13/18
to ang...@googlegroups.com
Thanks to both you Sir’s. it was very helpful

--
You received this message because you are subscribed to the Google Groups "Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.
--
Reply all
Reply to author
Forward
0 new messages