异常处理程序未捕获自定义中间件中的整个异常(.NET Core 2.0)
Exception Handler not catching whole exception in custom middleware (.NET Core 2.0)
我尝试使用中间件来处理异常。我写了一些代码,但它不工作。有问题。通常,如果我使用 try catch 范围,我可以捕获异常。但是我无法理解中间件。怎么了?
我在尝试范围内寻找结果实体。结果实体有异常所以我尝试使用 if 条件。我解决了这个问题,但我认为这是不可接受的:)
我在我的业务层抛出一些异常(洋葱架构)
例如:throw new Exception("have a problem");
还有我的中间件 class 在 webapi 层。如果我发现错误,我想发送响应一些数据。
下面是我的中间件代码
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
public ExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
try
{
var result = _next(httpContext);
if (result.Exception != null)
throw result.Exception;
return result;
}
catch (Exception ex)
{
return HandleExceptionAsync(httpContext, ex);
}
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = ResponseCode.Alliswell;
switch (exception)
{
case ServiceException _:
code = ResponseCode.ServiceError;
break;
case BusinessException _:
code = ResponseCode.BusinessError;
break;
case DataLayerException _:
code = ResponseCode.DataLayerError;
break;
case EsriServiceException _:
code = ResponseCode.EsriServiceError;
break;
}
//var result = JsonConvert.SerializeObject(new { error = ex.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var responseEntity = new Response<object>
{
Data = null,
Code = code,
ErrorMessage = exception.Message,
State = ResponseState.Error
};
var result = context.Response.WriteAsync(JsonConvert.SerializeObject(responseEntity));
return result;
}
}
在中间件中使用 async await 语法,以便捕获任何抛出的异常
public class ExceptionHandlingMiddleware {
private readonly RequestDelegate _next;
public ExceptionHandlingMiddleware(RequestDelegate next) {
_next = next;
}
public async Task Invoke(HttpContext httpContext) {
try {
await _next(httpContext);
} catch (Exception ex) {
await HandleExceptionAsync(httpContext, ex);
}
}
private async Task HandleExceptionAsync(HttpContext context, Exception exception) {
var code = ResponseCode.Alliswell;
switch (exception) {
case ServiceException _:
code = ResponseCode.ServiceError;
break;
case BusinessException _:
code = ResponseCode.BusinessError;
break;
case DataLayerException _:
code = ResponseCode.DataLayerError;
break;
case EsriServiceException _:
code = ResponseCode.EsriServiceError;
break;
}
//var result = JsonConvert.SerializeObject(new { error = ex.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var responseEntity = new Response<object> {
Data = null,
Code = code,
ErrorMessage = exception.Message,
State = ResponseState.Error
};
await context.Response.WriteAsync(JsonConvert.SerializeObject(responseEntity));
}
}
我尝试使用中间件来处理异常。我写了一些代码,但它不工作。有问题。通常,如果我使用 try catch 范围,我可以捕获异常。但是我无法理解中间件。怎么了?
我在尝试范围内寻找结果实体。结果实体有异常所以我尝试使用 if 条件。我解决了这个问题,但我认为这是不可接受的:)
我在我的业务层抛出一些异常(洋葱架构)
例如:throw new Exception("have a problem");
还有我的中间件 class 在 webapi 层。如果我发现错误,我想发送响应一些数据。
下面是我的中间件代码
public class ExceptionHandlingMiddleware
{
private readonly RequestDelegate _next;
public ExceptionHandlingMiddleware(RequestDelegate next)
{
_next = next;
}
public Task Invoke(HttpContext httpContext)
{
try
{
var result = _next(httpContext);
if (result.Exception != null)
throw result.Exception;
return result;
}
catch (Exception ex)
{
return HandleExceptionAsync(httpContext, ex);
}
}
private Task HandleExceptionAsync(HttpContext context, Exception exception)
{
var code = ResponseCode.Alliswell;
switch (exception)
{
case ServiceException _:
code = ResponseCode.ServiceError;
break;
case BusinessException _:
code = ResponseCode.BusinessError;
break;
case DataLayerException _:
code = ResponseCode.DataLayerError;
break;
case EsriServiceException _:
code = ResponseCode.EsriServiceError;
break;
}
//var result = JsonConvert.SerializeObject(new { error = ex.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var responseEntity = new Response<object>
{
Data = null,
Code = code,
ErrorMessage = exception.Message,
State = ResponseState.Error
};
var result = context.Response.WriteAsync(JsonConvert.SerializeObject(responseEntity));
return result;
}
}
在中间件中使用 async await 语法,以便捕获任何抛出的异常
public class ExceptionHandlingMiddleware {
private readonly RequestDelegate _next;
public ExceptionHandlingMiddleware(RequestDelegate next) {
_next = next;
}
public async Task Invoke(HttpContext httpContext) {
try {
await _next(httpContext);
} catch (Exception ex) {
await HandleExceptionAsync(httpContext, ex);
}
}
private async Task HandleExceptionAsync(HttpContext context, Exception exception) {
var code = ResponseCode.Alliswell;
switch (exception) {
case ServiceException _:
code = ResponseCode.ServiceError;
break;
case BusinessException _:
code = ResponseCode.BusinessError;
break;
case DataLayerException _:
code = ResponseCode.DataLayerError;
break;
case EsriServiceException _:
code = ResponseCode.EsriServiceError;
break;
}
//var result = JsonConvert.SerializeObject(new { error = ex.Message });
context.Response.ContentType = "application/json";
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var responseEntity = new Response<object> {
Data = null,
Code = code,
ErrorMessage = exception.Message,
State = ResponseState.Error
};
await context.Response.WriteAsync(JsonConvert.SerializeObject(responseEntity));
}
}