我正在尝试在API和客户端中创建集中式错误处理程序,但是角度拦截器从不捕获错误描述模型。拦截器中的错误消息始终为:“ URL的HTTP故障响应:0未知错误”。 API中的错误处理程序始终捕获所有错误和异常。当我使用邮递员时,错误模型会定期显示
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;
}));
}
任何人都知道我在做什么错。 谢谢