Reputation: 34689
Back with just jQuery, I would listen for any ajaxError events simply with:
$(document).on('ajaxError', someFunction);
But now with Angular2, I don't know how or where to put something like this (I was using it to make sure the user was still logged in)?
Is there a hook to all the services that may fail? Do I do it the same? Where would I put something like this? Very new to Angular2, any direction is appreciated.
Upvotes: 0
Views: 33
Reputation: 28592
In Angular2 you're using Http service to do all your Ajax calls.
So if we find a way to intercept it, we can log all the possible error s:
So one way is to wrap the Http base class :
@Injectable()
export class HttpService {
constructor(private http: Http) {}
get(url) {
let headers = new Headers();
return this.http.get(url, {
headers: headers
}).catch( this.handleError );
}
private handleError ( error : any ) {
consoe.log('do something with the error ');
return Observable.throw( errMsg );
}
}
And then obviously , instead of Http, you should use HttpService
Upvotes: 1