gpt4 book ai didi

javascript - Angular 拦截器 - 从 500 错误中获取消息

转载 作者:行者123 更新时间:2023-11-29 21:15:41 25 4
gpt4 key购买 nike

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

问题是,我在拦截器内的 responseError 中的 rejection.data 中获取了整个 HTML(下面的屏幕截图)。

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

这有可能吗?

Angular 拦截器:

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

最佳答案

I want to get the message from 500 errors (internal server error).

使用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);
}

来自文档:

The response object has these properties:

  • data – {string|Object} – The response body transformed with the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response.

-- AngularJS $http Service API Reference -- General Usage

关于javascript - Angular 拦截器 - 从 500 错误中获取消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39507809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com