Skip to content

Instantly share code, notes, and snippets.

@gnomeontherun
Last active November 4, 2020 06:46
Show Gist options
  • Save gnomeontherun/5678505 to your computer and use it in GitHub Desktop.
Save gnomeontherun/5678505 to your computer and use it in GitHub Desktop.

Revisions

  1. gnomeontherun renamed this gist May 30, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. gnomeontherun created this gist May 30, 2013.
    45 changes: 45 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    // Intercepting HTTP calls with AngularJS.
    angular.module('MyApp', [])
    .config(function ($provide, $httpProvider) {

    // Intercept http calls.
    $provide.factory('MyHttpInterceptor', function ($q) {
    return {
    // On request success
    request: function (config) {
    // console.log(config); // Contains the data about the request before it is sent.

    // Return the config or wrap it in a promise if blank.
    return config || $q.when(config);
    },

    // On request failure
    requestError: function (rejection) {
    // console.log(rejection); // Contains the data about the error on the request.

    // Return the promise rejection.
    return $q.reject(rejection);
    },

    // On response success
    response: function (response) {
    // console.log(response); // Contains the data from the response.

    // Return the response or promise.
    return response || $q.when(response);
    },

    // On response failture
    responseError: function (rejection) {
    // console.log(rejection); // Contains the data about the error.

    // Return the promise rejection.
    return $q.reject(rejection);
    }
    };
    });

    // Add the interceptor to the $httpProvider.
    $httpProvider.interceptors.push('LoggingHttpInterceptor');

    });