Created
August 29, 2016 11:11
-
-
Save jadjoubran/83822f4ed9723203929c5321feb1c694 to your computer and use it in GitHub Desktop.
Angular error interceptors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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