Skip to content

Instantly share code, notes, and snippets.

@arcdev1
Forked from abyx/angular-error-handling.js
Last active October 3, 2015 03:21
Show Gist options
  • Save arcdev1/1bf00af0e91381806ff5 to your computer and use it in GitHub Desktop.
Save arcdev1/1bf00af0e91381806ff5 to your computer and use it in GitHub Desktop.
/**
* This Gist has been modified to use a custom flag on the config object instead of an HTTP Header in order to
* indicate wether or not to specifically handle the error.
* THIS CODE IS UNTESTED!!
*/
//var HEADER_NAME = 'MyApp-Handle-Errors-Generically'; // We don't want to use Headers, we use a flag on the config object instead
var specificallyHandleInProgress = false;
angular.module('myApp').factory('RequestsErrorHandler', ['$q', function($q) {
return {
// --- The user's API for claiming responsiblity for requests ---
specificallyHandled: function(specificallyHandledBlock) {
specificallyHandleInProgress = true;
try {
return specificallyHandledBlock();
} finally {
specificallyHandleInProgress = false;
}
},
// --- Response interceptor for handling errors generically ---
responseError: function(rejection) {
// Instead of checking for an HTTP header we check for config.genericallyHandle
var shouldHandle = (rejection && rejection.config && rejection.config.genericallyHandle);
if (shouldHandle) {
// --- Your generic error handling goes here ---
}
return $q.reject(rejection);
}
};
}]);
angular.module('myApp').config(['$provide', '$httpProvider', function($provide, $httpProvider) {
$httpProvider.interceptors.push('RequestsErrorHandler');
// --- Decorate $http to add a special header by default ---
function addHandlerFlagToConfig(config) {
config = config || {};
//config.headers = config.headers || {};
// Add the header unless user asked to handle errors himself
if (specificallyHandleInProgress) {
config.genericallyHandle = false;
} else {
config.genericallyHandle = true;
}
return config;
}
// The rest here is mostly boilerplate needed to decorate $http safely
$provide.decorator('$http', ['$delegate', function($delegate) {
function decorateRegularCall(method) {
return function(url, config) {
return $delegate[method](url, addHeaderToConfig(config));
};
}
function decorateDataCall(method) {
return function(url, data, config) {
return $delegate[method](url, data, addHandlerFlagToConfig(config));
};
}
function copyNotOverriddenAttributes(newHttp) {
for (var attr in $delegate) {
if (!newHttp.hasOwnProperty(attr)) {
if (typeof($delegate[attr]) === 'function') {
newHttp[attr] = function() {
return $delegate.apply($delegate, arguments);
};
} else {
newHttp[attr] = $delegate[attr];
}
}
}
}
var newHttp = function(config) {
return $delegate(addHandlerFlagToConfig(config));
};
newHttp.get = decorateRegularCall('get');
newHttp.delete = decorateRegularCall('delete');
newHttp.head = decorateRegularCall('head');
newHttp.jsonp = decorateRegularCall('jsonp');
newHttp.post = decorateDataCall('post');
newHttp.put = decorateDataCall('put');
copyNotOverriddenAttributes(newHttp);
return newHttp;
}]);
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment