You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.3 KiB
41 lines
1.3 KiB
3 years ago
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
using Microsoft.Extensions.Options;
|
||
|
using System;
|
||
|
using Volo.Abp.ExceptionHandling;
|
||
|
|
||
|
namespace Sanhe.Abp.Wrapper;
|
||
|
|
||
|
public class DefaultExceptionWrapHandler : IExceptionWrapHandler
|
||
|
{
|
||
|
public void Wrap(ExceptionWrapContext context)
|
||
|
{
|
||
|
if (context.Exception is IHasErrorCode exceptionWithErrorCode)
|
||
|
{
|
||
|
string errorCode;
|
||
|
if (!exceptionWithErrorCode.Code.IsNullOrWhiteSpace() &&
|
||
|
exceptionWithErrorCode.Code.Contains(":"))
|
||
|
{
|
||
|
errorCode = exceptionWithErrorCode.Code.Split(':')[1];
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
errorCode = exceptionWithErrorCode.Code;
|
||
|
}
|
||
|
|
||
|
context.WithCode(errorCode);
|
||
|
}
|
||
|
|
||
|
// 没有处理的异常代码统一用配置代码处理
|
||
|
if (context.ErrorInfo.Code.IsNullOrWhiteSpace())
|
||
|
{
|
||
|
if (context.StatusCode.HasValue)
|
||
|
{
|
||
|
context.WithCode(((int)context.StatusCode).ToString());
|
||
|
return;
|
||
|
}
|
||
|
var wrapperOptions = context.ServiceProvider.GetRequiredService<IOptions<AbpWrapperOptions>>().Value;
|
||
|
context.WithCode(wrapperOptions.CodeWithUnhandled);
|
||
|
}
|
||
|
}
|
||
|
}
|