Skip to content

Instantly share code, notes, and snippets.

@droyad
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save droyad/8757f98f05eb00d99d48 to your computer and use it in GitHub Desktop.

Select an option

Save droyad/8757f98f05eb00d99d48 to your computer and use it in GitHub Desktop.
Angular Error handling
export function httpErrorHandler($httpProvider: ng.IHttpProvider) {
var interceptor = ($q: ng.IQService) => {
return {
'requestError': response => {
toastr.error(response.message || response.statusText || "An error occured");
return $q.reject(response);
},
'responseError': response => {
var error = (response.data && response.data.errors) || response.statusText || "An error occured";
if (_.isArray(error)) {
if (error.length === 1)
error = error[0];
else
error = "<ul><li>" + error.join('</li><li>') + "</li></ul>";
}
toastr.error(error);
return $q.reject(response);
}
}
}
$httpProvider.interceptors.push(interceptor);
};
.config(httpErrorHandler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment