|
|
|
@ -17,74 +17,73 @@ using Volo.Abp.ExceptionHandling;
|
|
|
|
|
using Volo.Abp.Http; |
|
|
|
|
using Volo.Abp.Json; |
|
|
|
|
|
|
|
|
|
namespace Sanhe.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling |
|
|
|
|
namespace Sanhe.Abp.AspNetCore.Mvc.Wrapper.ExceptionHandling; |
|
|
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
|
[ExposeServices(typeof(AbpExceptionFilter))] |
|
|
|
|
public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDependency |
|
|
|
|
{ |
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
|
[ExposeServices(typeof(AbpExceptionFilter))] |
|
|
|
|
public class AbpExceptionWrapResultFilter : AbpExceptionFilter, ITransientDependency |
|
|
|
|
protected async override Task HandleAndWrapException(ExceptionContext context) |
|
|
|
|
{ |
|
|
|
|
protected async override Task HandleAndWrapException(ExceptionContext context) |
|
|
|
|
{ |
|
|
|
|
var wrapResultChecker = context.GetRequiredService<IWrapResultChecker>(); |
|
|
|
|
|
|
|
|
|
if (!wrapResultChecker.WrapOnException(context)) |
|
|
|
|
{ |
|
|
|
|
await base.HandleAndWrapException(context); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
var wrapResultChecker = context.GetRequiredService<IWrapResultChecker>(); |
|
|
|
|
|
|
|
|
|
//TODO: Trigger an AbpExceptionHandled event or something like that. |
|
|
|
|
var wrapOptions = context.GetRequiredService<IOptions<AbpWrapperOptions>>().Value; |
|
|
|
|
var exceptionHandlingOptions = context.GetRequiredService<IOptions<AbpExceptionHandlingOptions>>().Value; |
|
|
|
|
var exceptionToErrorInfoConverter = context.GetRequiredService<IExceptionToErrorInfoConverter>(); |
|
|
|
|
var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, options => |
|
|
|
|
{ |
|
|
|
|
options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; |
|
|
|
|
options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients; |
|
|
|
|
}); |
|
|
|
|
if (!wrapResultChecker.WrapOnException(context)) |
|
|
|
|
{ |
|
|
|
|
await base.HandleAndWrapException(context); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var logLevel = context.Exception.GetLogLevel(); |
|
|
|
|
//TODO: Trigger an AbpExceptionHandled event or something like that. |
|
|
|
|
var wrapOptions = context.GetRequiredService<IOptions<AbpWrapperOptions>>().Value; |
|
|
|
|
var exceptionHandlingOptions = context.GetRequiredService<IOptions<AbpExceptionHandlingOptions>>().Value; |
|
|
|
|
var exceptionToErrorInfoConverter = context.GetRequiredService<IExceptionToErrorInfoConverter>(); |
|
|
|
|
var remoteServiceErrorInfo = exceptionToErrorInfoConverter.Convert(context.Exception, options => |
|
|
|
|
{ |
|
|
|
|
options.SendExceptionsDetailsToClients = exceptionHandlingOptions.SendExceptionsDetailsToClients; |
|
|
|
|
options.SendStackTraceToClients = exceptionHandlingOptions.SendStackTraceToClients; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
var remoteServiceErrorInfoBuilder = new StringBuilder(); |
|
|
|
|
remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); |
|
|
|
|
remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true)); |
|
|
|
|
var logLevel = context.Exception.GetLogLevel(); |
|
|
|
|
|
|
|
|
|
var logger = context.GetService<ILogger<AbpExceptionWrapResultFilter>>(NullLogger<AbpExceptionWrapResultFilter>.Instance); |
|
|
|
|
var remoteServiceErrorInfoBuilder = new StringBuilder(); |
|
|
|
|
remoteServiceErrorInfoBuilder.AppendLine($"---------- {nameof(RemoteServiceErrorInfo)} ----------"); |
|
|
|
|
remoteServiceErrorInfoBuilder.AppendLine(context.GetRequiredService<IJsonSerializer>().Serialize(remoteServiceErrorInfo, indented: true)); |
|
|
|
|
|
|
|
|
|
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); |
|
|
|
|
var logger = context.GetService<ILogger<AbpExceptionWrapResultFilter>>(NullLogger<AbpExceptionWrapResultFilter>.Instance); |
|
|
|
|
|
|
|
|
|
logger.LogException(context.Exception, logLevel); |
|
|
|
|
logger.LogWithLevel(logLevel, remoteServiceErrorInfoBuilder.ToString()); |
|
|
|
|
|
|
|
|
|
await context.GetRequiredService<IExceptionNotifier>().NotifyAsync(new ExceptionNotificationContext(context.Exception)); |
|
|
|
|
logger.LogException(context.Exception, logLevel); |
|
|
|
|
|
|
|
|
|
if (context.Exception is AbpAuthorizationException) |
|
|
|
|
{ |
|
|
|
|
await context.GetRequiredService<IAbpAuthorizationExceptionHandler>() |
|
|
|
|
.HandleAsync(context.Exception.As<AbpAuthorizationException>(), context.HttpContext); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
var statusCodFinder = context.GetRequiredService<IHttpExceptionStatusCodeFinder>(); |
|
|
|
|
var exceptionWrapHandler = context.GetRequiredService<IExceptionWrapHandlerFactory>(); |
|
|
|
|
|
|
|
|
|
var exceptionWrapContext = new ExceptionWrapContext( |
|
|
|
|
context.Exception, |
|
|
|
|
remoteServiceErrorInfo, |
|
|
|
|
context.HttpContext.RequestServices, |
|
|
|
|
statusCodFinder.GetStatusCode(context.HttpContext, context.Exception)); |
|
|
|
|
|
|
|
|
|
exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext); |
|
|
|
|
|
|
|
|
|
context.Result = new ObjectResult(new WrapResult( |
|
|
|
|
exceptionWrapContext.ErrorInfo.Code, |
|
|
|
|
exceptionWrapContext.ErrorInfo.Message, |
|
|
|
|
exceptionWrapContext.ErrorInfo.Details)); |
|
|
|
|
await context.GetRequiredService<IExceptionNotifier>().NotifyAsync(new ExceptionNotificationContext(context.Exception)); |
|
|
|
|
|
|
|
|
|
context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true"); |
|
|
|
|
context.HttpContext.Response.StatusCode = (int)wrapOptions.HttpStatusCode; |
|
|
|
|
} |
|
|
|
|
if (context.Exception is AbpAuthorizationException) |
|
|
|
|
{ |
|
|
|
|
await context.GetRequiredService<IAbpAuthorizationExceptionHandler>() |
|
|
|
|
.HandleAsync(context.Exception.As<AbpAuthorizationException>(), context.HttpContext); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
var statusCodFinder = context.GetRequiredService<IHttpExceptionStatusCodeFinder>(); |
|
|
|
|
var exceptionWrapHandler = context.GetRequiredService<IExceptionWrapHandlerFactory>(); |
|
|
|
|
|
|
|
|
|
var exceptionWrapContext = new ExceptionWrapContext( |
|
|
|
|
context.Exception, |
|
|
|
|
remoteServiceErrorInfo, |
|
|
|
|
context.HttpContext.RequestServices, |
|
|
|
|
statusCodFinder.GetStatusCode(context.HttpContext, context.Exception)); |
|
|
|
|
|
|
|
|
|
exceptionWrapHandler.CreateFor(exceptionWrapContext).Wrap(exceptionWrapContext); |
|
|
|
|
|
|
|
|
|
context.Result = new ObjectResult(new WrapResult( |
|
|
|
|
exceptionWrapContext.ErrorInfo.Code, |
|
|
|
|
exceptionWrapContext.ErrorInfo.Message, |
|
|
|
|
exceptionWrapContext.ErrorInfo.Details)); |
|
|
|
|
|
|
|
|
|
context.Exception = null; //Handled! |
|
|
|
|
context.HttpContext.Response.Headers.Add(AbpHttpWrapConsts.AbpWrapResult, "true"); |
|
|
|
|
context.HttpContext.Response.StatusCode = (int)wrapOptions.HttpStatusCode; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
context.Exception = null; //Handled! |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|