2016-09-15 93 views
0

我正在使用角拦截器,并且我想从500个错误(内部服务器错误)中获取消息角拦截器 - 从500错误中获取消息

的问题是,我得到的rejection.data整个HTML中responseError内拦截器(下图)。

我读过,我必须配置web.config但我仍然得到整个HTML。我只想得到消息。

有没有可能这样做?

角拦截:

app.config(['$httpProvider', function ($httpProvider) { 

    $httpProvider.interceptors.push(function ($q, $rootScope) { 

     return { 
      request: function (config) { 

       //the same config/modified config/a new config needs to be returned. 
       return config; 
      }, 
      requestError: function (rejection) { 

       //Initializing error list 
       if ($rootScope.errorList == undefined) { 
        $rootScope.errorList = []; 
       } 

       $rootScope.errorList.push(rejection.data); 

       //It has to return the rejection, simple reject call doesn't work 
       return $q.reject(rejection); 
      }, 
      response: function (response) { 

       //the same response/modified/or a new one need to be returned. 
       return response; 
      }, 
      responseError: function (rejection) { 

       //Initializing the error list 
       if ($rootScope.errorList == undefined) { 
        $rootScope.errorList = []; 
       } 

       //Adding to error list 
       $rootScope.errorList.push(rejection.data); 

       //It has to return the rejection, simple reject call doesn't work 
       return $q.reject(rejection); 
      } 
     }; 
    }); 
}]); 

的Web.Config

<system.webServer> 
    <httpErrors existingResponse="PassThrough" errorMode="Detailed"></httpErrors> 
</system.webServer> 

Image

编辑: 我想从异常快照消息

Image2

+0

你这是在data.message得到些什么? – Disha

+0

什么都没有。因为在数据中我有像第一张图片那样的整个HTML。我做了一个编辑 - 看看。 –

回答

0

我想从500错误(内部服务器错误)中获取消息。

使用response.statusText得到消息:

responseError: function (errorResponse) { 

    //Initializing the error list 
    if ($rootScope.errorList == undefined) { 
     $rootScope.errorList = []; 
    } 

    //Adding to error list 
    $rootScope.errorList.push(errorResponse.statusText); 

    //It has to return the rejection, simple reject call doesn't work 
    return $q.reject(errorResponse); 
} 

从文档:

响应对象具有以下属性:

  • 数据 - {string|Object} - 响应正文用变换函数进行变换。
  • status - {number} - 响应的HTTP状态码。
  • 标头 - {function([headerName])} - 标头吸气功能。
  • config - {Object} - 用于生成请求的配置对象。
  • statusText - {string} - 响应的HTTP状态文本。

- AngularJS $http Service API Reference -- General Usage