JavaScript AngularJS Interceptor ResponseError response undefined angularjs interceptor responseerror,angularjs http interceptor responseerror,angularjs interceptor response

I have this handler that I built to handle $http requests.

It looks like this:

.service('ApiHandler', ['$q', '$http', '$injector', 'apiUrl', 'ErrorService', 'toastr', function ($q, $http, $injector, apiUrl, errorService, toastr) {

    // Private function to build our request
    var buildRequest = function (url, method, data, params) {

        // Create our deferred promise
        var deferred = $q.defer();

        // Create the model
        var model = {
            method: method,
            url: apiUrl + url,
            data: data,
            params: params
        };

        console.log(model);

        // Build our request
        $http(model).then(function (response) {

            console.log('we have a response');

            // Resolve our response
            deferred.resolve(response.data || response);

        // If we have an error
        }, function (error) {

            console.log('we have an error');

            // Process our error
            processedError = errorService.process(error);

            // Display our error
            toastr.error(processedError.message, processedError.title);
        });

        // Return our promise
        return deferred.promise;
    };

    // GET
    this.get = function (url, params) {
        return buildRequest(url, 'GET', null, params);
    };

    // POST
    this.post = function (url, data) {
        return buildRequest(url, 'POST', data);
    };

    // PUT
    this.put = function (url, data) {
        return buildRequest(url, 'PUT', data);
    };

    // DELETE
    this.delete = function (url, data) {
        return buildRequest(url, 'DELETE', data);
    };
}])

The idea is that when someone performs any http request, if the request is successful it will display the data (only the data) and if it errors for what ever reason, it will process the error and display a nice error message.

This was working, but I am unsure why it has stopped. I purposefully put an error in my API to force a 500 Internal Server Error and in chrome I can see this:

GET http://localhost:54326/orders?orderNumber=M0002663&readCoreHistory=true 500 (Internal Server Error)

The problem is, after I get this error, I expect to see we have an error in the console, but I don't. I get we have a response.

Does anyone know why the success handler is being hit rather than the error handler?

Update

I now know what was causing the issue. I had an interceptor that looked like this:

.factory('AuthInterceptorService', ['$location', function ($location) {

    // The request function
    var request = function (config) {

        // Get our stored auth data
        var authData = angular.fromJson(sessionStorage.authorizationData);

        // Set our headers to the request headers or a new object
        config.headers = config.headers || {};

        // If we have any auth data
        if (authData) {

            // Set our authorization header
            config.headers.Authorization = 'Bearer ' + authData.token;
        }

        // Return our config
        return config;
    };

    // The response function
    var responseError = function (response) {

        console.log('error handler');

        // If our response status is unauthorized
        if (response.status === 401) {

            // Redirect to the login screen
            $location.path('/security/login');
        }
    };

    return {
        request: request,
        responseError: responseError
    };
}])

As you the responseError function is not returning / rejecting anything, so I changed it to this:

.factory('AuthInterceptorService', ['$q', '$location', 'ErrorService', function ($q, $location, errorService) {

    // The request function
    var request = function (config) {

        // Get our stored auth data
        var authData = angular.fromJson(sessionStorage.authorizationData);

        // Set our headers to the request headers or a new object
        config.headers = config.headers || {};

        // If we have any auth data
        if (authData) {

            // Set our authorization header
            config.headers.Authorization = 'Bearer ' + authData.token;
        }

        // Return our config
        return config;
    };

    // The response function
    var responseError = function (response) {

        console.log('error handler');

        // If our response status is unauthorized
        if (response.status === 401) {

            // Redirect to the login screen
            $location.path('/security/login');
        } else {

            // Process our error
            var error = errorService.process(response);

            console.log(error);

            // Reject our response
            return $q.reject(error);
        }
    };

    return {
        request: request,
        responseError: responseError
    };
}])

And I changed my API handler to this:

.service('ApiHandler', ['$q', '$http', 'apiUrl', 'toastr', function ($q, $http, apiUrl, toastr) {

    // Private function to build our request
    var buildRequest = function (url, method, data, params) {

        // Create our deferred promise
        var deferred = $q.defer();

        // Create the model
        var model = {
            method: method,
            url: apiUrl + url,
            data: data,
            params: params
        };

        //console.log(model);

        // Build our request
        $http(model).success(function (response) {

            //console.log('we have a response');

            // Resolve our response
            deferred.resolve(response);

        // If we have an error
        }).error(function (error) {

            console.log('we have an error');

            console.log(error);

            // Display our error
            toastr.error(error.message, error.title);

            deferred.reject();
        });

        // Return our promise
        return deferred.promise;
    };

    // GET
    this.get = function (url, params) {
        return buildRequest(url, 'GET', null, params);
    };

    // POST
    this.post = function (url, data) {
        return buildRequest(url, 'POST', data);
    };

    // PUT
    this.put = function (url, data) {
        return buildRequest(url, 'PUT', data);
    };

    // DELETE
    this.delete = function (url, data) {
        return buildRequest(url, 'DELETE', data);
    };
}])

Now the error handler is actually being invoked, but the error parameter is undefined. Does anyone know why?

Answer:1

Could you try doing like that :

$http.get('/someURL').success(function(data, status, header, config) {
    // success handler
}).error(function(data, status, header, config) {
    // error handler
});
Answer:2

My first question here :) I'm porting my site to Angular (I'm new to it) and wondering if I'd be able to keep my pure CSS modal dialogue box while using ui-router. In HTML it looks like this: &...

My first question here :) I'm porting my site to Angular (I'm new to it) and wondering if I'd be able to keep my pure CSS modal dialogue box while using ui-router. In HTML it looks like this: &...

I have two arrays which I want to merge. var arrayOne = [{id:1},{id:2}]; var arrayTwo = [{age: 20}, {age: 25}, {age: 22}]; Merged array should be like this [{id:1, age: 20}, {id:2, age: 25}, {age: ...

I have two arrays which I want to merge. var arrayOne = [{id:1},{id:2}]; var arrayTwo = [{age: 20}, {age: 25}, {age: 22}]; Merged array should be like this [{id:1, age: 20}, {id:2, age: 25}, {age: ...

  1. merge array objects javascript
  2. merge array objects lodash
  3. merge array objects
  4. merge array objects php
  5. merge array objects es6
  6. concat array objects javascript
  7. concat array objects
  8. merge two array objects in javascript
  9. merge 2 array objects javascript
  10. merge array of objects by key
  11. merge array of objects js
  12. merge array of objects javascript es6
  13. merge array of objects without duplicates javascript
  14. merge array of objects typescript
  15. merge two array objects in php
  16. merge array of objects by property
  17. merge array of objects by key js
  18. merge two array objects
  19. merge array of objects angular
  20. jquery merge array objects

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. My code currently is this: var nextScroll; setInterval(function() { ...

I am creating a self scrolling, auto updating table and need to search through through elements of a class from a certain point. My code currently is this: var nextScroll; setInterval(function() { ...

  1. jquery loop through array
  2. jquery loop through elements
  3. jquery loop through json
  4. jquery loop through table rows
  5. jquery loop through elements by class
  6. jquery loop through select options
  7. jquery loop through json array
  8. jquery loop through child elements
  9. jquery loop through checkboxes
  10. jquery loop through json object
  11. jquery loop through form elements
  12. jquery loop through array of objects
  13. jquery loop through list
  14. jquery loop through dictionary
  15. jquery loop through table
  16. jquery loop through checked checkboxes
  17. jquery loop through inputs
  18. jquery loop through elements by name
  19. jquery loop through td in tr
  20. jquery loop through divs

How can I get the value of input on click using vanilla javascript? function getvalue() { console.log(this.val); } <input type="text" onclick="getvalue()" value="asdf"></input> &...

How can I get the value of input on click using vanilla javascript? function getvalue() { console.log(this.val); } <input type="text" onclick="getvalue()" value="asdf"></input> &...

  1. value from input javascript
  2. value from input field javascript
  3. value from input angular
  4. value from input jquery
  5. value from input
  6. value from input php
  7. value from input html
  8. value from input field
  9. value from input text
  10. value from input text javascript
  11. value from input type text
  12. value from input radio
  13. value from input java
  14. value from input box
  15. get value from input javascript
  16. get value from input jquery
  17. get value from input
  18. get value from input js
  19. get value from input react
  20. get value from input angular