Skip to content

Instantly share code, notes, and snippets.

@jadjoubran
Created August 29, 2016 11:11
Show Gist options
  • Save jadjoubran/83822f4ed9723203929c5321feb1c694 to your computer and use it in GitHub Desktop.
Save jadjoubran/83822f4ed9723203929c5321feb1c694 to your computer and use it in GitHub Desktop.
Angular error interceptors
export class APIService {
constructor(Restangular, ToastService) {
'ngInject';
var headers = {
'Content-Type': 'application/json',
'Accept': 'application/x.laravel.v1+json'
};
return Restangular.withConfig(function(RestangularConfigurer) {
RestangularConfigurer
.setBaseUrl('/api/')
.setDefaultHeaders(headers)
.setErrorInterceptor(function(response) {
if (response.status === 422) {
for (var error in response.data.errors) {
var error_message = response.data.errors[error][0];
return ToastService.error(error_message);
}
}
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment