Skip to content

Instantly share code, notes, and snippets.

@abyx
Last active October 9, 2025 16:10
Show Gist options
  • Select an option

  • Save abyx/acd46b6513e2c58ed1d3 to your computer and use it in GitHub Desktop.

Select an option

Save abyx/acd46b6513e2c58ed1d3 to your computer and use it in GitHub Desktop.

Revisions

  1. abyx revised this gist Oct 26, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion angular-error-handling.js
    Original file line number Diff line number Diff line change
    @@ -63,7 +63,7 @@ angular.module('myApp').config(['$provide', '$httpProvider', function($provide,
    if (!newHttp.hasOwnProperty(attr)) {
    if (typeof($delegate[attr]) === 'function') {
    newHttp[attr] = function() {
    return $delegate.apply($delegate, arguments);
    return $delegate[attr].apply($delegate, arguments);
    };
    } else {
    newHttp[attr] = $delegate[attr];
  2. abyx revised this gist Jun 27, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion angular-error-handling.js
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,7 @@ angular.module('myApp').config(['$provide', '$httpProvider', function($provide,
    return config;
    }

    // The rest here si mostly boilerplate needed to decorate $http safely
    // The rest here is mostly boilerplate needed to decorate $http safely
    $provide.decorator('$http', ['$delegate', function($delegate) {
    function decorateRegularCall(method) {
    return function(url, config) {
  3. abyx created this gist Jun 27, 2014.
    91 changes: 91 additions & 0 deletions angular-error-handling.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    var HEADER_NAME = 'MyApp-Handle-Errors-Generically';
    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) {
    var shouldHandle = (rejection && rejection.config && rejection.config.headers
    && rejection.config.headers[HEADER_NAME]);

    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 addHeaderToConfig(config) {
    config = config || {};
    config.headers = config.headers || {};

    // Add the header unless user asked to handle errors himself
    if (!specificallyHandleInProgress) {
    config.headers[HEADER_NAME] = true;
    }

    return config;
    }

    // The rest here si 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, addHeaderToConfig(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(addHeaderToConfig(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;
    }]);
    }]);