N
N
nuclear_kote2016-05-19 21:50:29
Angular
nuclear_kote, 2016-05-19 21:50:29

Whether probably to change result of request in interceptor'e?

For example, if the request fell with a timeout, replace the response with some other one.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Yarkov, 2016-05-19
@nuclear_kote

/*global angular*/
(function () {
  'use strict';

  angular
    .module('App')
    .factory('httpErrorResponceInterceptor', ['$q', '$injector', '$rootScope', '$log',
                          httpErrorResponceInterceptor]);

  /**
   * Интерсептор для перехвата ответов сервера, которые содержат ошибку.
   * Реализует обработку ошибочных ответов сервера (http кодов).
   * Реагирует только на коды 400, 401, 403, 404, и 500
   *
   * @name httpErrorResponceInterceptor
   */
  function httpErrorResponceInterceptor($q, $injector, $rootScope, $log) {
    var self = {};

    self.responseError = function (response) {
      if (response.status == 401){
        return $q.reject(response);
      }
      else if (response.status == 400){
        return $q.reject(response);
      }
      else if (response.status == 403) {
        return $q.reject(response);
      }
      else if (response.status == 404) {
        return $q.reject(response);
      }
      else if (response.status == 500) {
        return $q.reject(response);
      }
      
      return $http(response.config);
    };

    return self;
  }

})();

Change the response object and return ))
I cut it out of the project and removed the extra code, so a bunch of ifs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question