当我在服务器端代码中抛出HttpResponseException时,我似乎无法找到检索错误消息的方法。
这是我用来捕获未处理的服务器端异常的全局异常过滤器:
public class ExceptionHandlingAttribute : ExceptionFilterAttribute
{
private static readonly Logger Nlog = LogManager.GetCurrentClassLogger();
public override void OnException(HttpActionExecutedContext context)
{
string message = context.Exception.Message;
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
{
Content = new StringContent(message),
ReasonPhrase = "Exception"
});
}
}
这是我的客户端代码(angular2):
getAll(): Observable<T[]> {
let url_ = `${this.localUrl}`;
return this.http.request(url_
).map((response) => {
return this.processArrayResponse(response);
}).catch((response: any, caught: any) => {
console.log(response)
return this.handleArrayResponseError(response);
});
}
protected handleAnyResponseError(response: any): Observable<any> {
if (response instanceof Response) {
try {
return Observable.of(this.processAnyResponse(response));
} catch (e) {
console.log(e)
return <Observable<any>><any>Observable.throw(e);
}
} else
return <Observable<any>><any>Observable.throw(response);
}
我得到的回应是:
Response {_body: ArrayBuffer, status: 500, ok: false, statusText: "Exception", headers: Headers…}
headers:Headers
ok:false
status:500
statusText:"Exception"
type:2
url:"http://localhost:83/api/PreHeatSchedule/getExportFile"
_body:ArrayBuffer
__proto__: Body
我不知道如何从客户端的异常中获取Content部分。