7

Is there an API similar to angular 1.x's Interceptor API?

I want to be able to add interceptors at application startup that will

  • show the login dialog whenever a 401 status code is returned
  • show a generic error message whenever a 5xx status code is returned

for every http request the application makes.

1 Answer 1

3

You can create your own HTTPConnection that handles error and inject it into the root app component at bootstrap time.

export class CustomHTTPConnection implements Connection
{
}

and then inject it while bootstrapping as follows

bootstrap([provider(Connection,{useClass:CustomHTTPConnection}]);

If you want do not want to provide a custom connection class, you can do it for each individual request as Http returns an observable which as 3 parameters: onNext, onError, onCompleted.

You can use it as follows:

class Component
{
constructor(private httpService:HttpService){
}
onInit(){
 this.httpService.getData().subscribe(
  ()=>{},  //on Next
  ()=>{}   //onError
}
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.