c# - 在 ANGULAR 拦截器中捕获错误消息
问题描述
我正在尝试在 API 和客户端创建集中式错误处理程序,但角度拦截器永远不会捕获错误描述模型。拦截器中的错误消息始终是:
“url 的 Http 失败响应:0 未知错误”。
API 中的错误处理程序始终捕获每个错误和异常。当我使用 Postman 时,会定期显示错误模型。
app.UseExceptionHandler(errorApp =>
{
errorApp.Run(async context =>
{
var errorFeature = context.Features.Get<IExceptionHandlerFeature>();
var exception = errorFeature.Error;
var problemDetails = new ProblemDetails
{
Instance = $"urn:myorganization:error:{Guid.NewGuid()}"
};
if (exception is BadHttpRequestException badHttpRequestException)
{
problemDetails.Title = "Invalid request";
problemDetails.Status = (int)typeof(BadHttpRequestException).GetProperty("StatusCode",
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(badHttpRequestException);
problemDetails.Detail = badHttpRequestException.Message;
}
else
{
problemDetails.Title = "An unexpected error occurred!";
problemDetails.Status = 500;
problemDetails.Detail = exception.Message.ToString();
}
// log the exception etc..
context.Response.StatusCode = problemDetails.Status.Value;
context.Response.WriteJson(problemDetails, "application/problem+json");
});
});
这是角度拦截器:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
delay(0),
map((event: HttpEvent<any>) => {
this.Loader = true;
return event;
}),
catchError((error: any) => {
console.log('error--->>>', error);
return throwError(error);
}),
finalize(() =>{
this.Loader = false;
}));
}
有人知道我做错了什么吗?
解决方案
推荐阅读
- java - Spring RequestMapping 不会在 .jsp 请求上触发
- ios - 在使用 swift 的 iOS 中,如何创建一个始终位于整个应用程序顶部的可拖动和浮动 UIView
- python - Jupyter notebook 多窗口输出
- ios - 用动画在 UIImageView 上显示多个图像
- android - Android:使用 webview 捕获图像
- android - 使用滑行从 gif 图像中删除透明的白色背景?
- firebase - Firebase 规则不起作用。即使我在 Firestore 中设置规则也返回数据
- ios - 使用 swift 在 iOS 上本地保存和检索照片及其所有元数据
- spring-boot - PlatformApplicationDisabledException
- python - 在 python 上使用 mysql 时出现错误,例如 from flask.ext.mysql import MySQL