using System; using System.Collections.Generic; using System.Net; using Volo.Abp.Collections; namespace Sanhe.Abp.Wrapper; public class AbpWrapperOptions { /// /// 未处理异常代码 /// 默认: 500 /// public string CodeWithUnhandled { get; set; } /// /// 是否启用包装器 /// public bool IsEnabled { get; set; } /// /// 成功时返回代码 /// 默认:0 /// public string CodeWithSuccess { get; set; } /// /// 资源为空时是否提示错误 /// 默认: false /// public bool ErrorWithEmptyResult { get; set; } /// /// 资源为空时返回代码 /// 默认:404 /// public Func CodeWithEmptyResult { get; set; } /// /// 资源为空时返回错误消息 /// public Func MessageWithEmptyResult { get; set; } /// /// 包装后的返回状态码 /// 默认:200 HttpStatusCode.OK /// public HttpStatusCode HttpStatusCode { get; set; } /// /// 忽略Url开头类型 /// public IList IgnorePrefixUrls { get; } /// /// 忽略指定命名空间 /// public IList IgnoreNamespaces { get; } /// /// 忽略控制器 /// public ITypeList IgnoreControllers { get; } /// /// 忽略返回值 /// public ITypeList IgnoreReturnTypes { get; } /// /// 忽略异常 /// public ITypeList IgnoreExceptions { get; } /// /// 忽略接口类型 /// public ITypeList IgnoredInterfaces { get; } internal IDictionary ExceptionHandles { get; } public AbpWrapperOptions() { CodeWithUnhandled = "500"; CodeWithSuccess = "0"; HttpStatusCode = HttpStatusCode.OK; ErrorWithEmptyResult = false; IgnorePrefixUrls = new List(); IgnoreNamespaces = new List(); IgnoreControllers = new TypeList(); IgnoreReturnTypes = new TypeList(); IgnoredInterfaces = new TypeList() { typeof(IWrapDisabled) }; IgnoreExceptions = new TypeList(); CodeWithEmptyResult = (_) => "404"; MessageWithEmptyResult = (_) => "Not Found"; ExceptionHandles = new Dictionary(); } public void AddHandler(IExceptionWrapHandler handler) where TException : Exception { AddHandler(typeof(TException), handler); } public void AddHandler(Type exceptionType, IExceptionWrapHandler handler) { ExceptionHandles[exceptionType] = handler; } public IExceptionWrapHandler GetHandler(Type exceptionType) { ExceptionHandles.TryGetValue(exceptionType, out IExceptionWrapHandler handler); return handler; } }