using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.AspNetCore.Http; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Text; namespace Utility { /// /// 抛异常静态类 /// public static class Oops { /// /// 错误代码类型 /// private static readonly IEnumerable ErrorCodeTypes; /// /// 构造函数 /// static Oops() { ErrorCodeTypes = GetErrorCodeTypes(); } /// /// 抛出业务异常信息 /// /// 异常消息 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Bah(string errorMessage, params object[] args) { var friendlyException = Oh(errorMessage, typeof(ValidationException), args); friendlyException.StatusCode = StatusCodes.Status400BadRequest; friendlyException.ValidationException = true; return friendlyException; } /// /// 抛出业务异常信息 /// /// 错误码 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Bah(object errorCode, params object[] args) { var friendlyException = Oh(errorCode, typeof(ValidationException), args); friendlyException.StatusCode = StatusCodes.Status400BadRequest; friendlyException.ValidationException = true; return friendlyException; } /// /// 抛出字符串异常 /// /// 异常消息 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Oh(string errorMessage, params object[] args) { var friendlyException = new AppFriendlyException(errorMessage, default); friendlyException.StatusCode = StatusCodes.Status400BadRequest; friendlyException.ValidationException = true; return friendlyException; } /// /// 抛出字符串异常 /// /// 异常消息 /// 具体异常类型 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Oh(string errorMessage, Type exceptionType, params object[] args) { var exceptionMessage = string.Format(errorMessage, args); return new AppFriendlyException(exceptionMessage, default, Activator.CreateInstance(exceptionType, new object[] { exceptionMessage }) as Exception); } /// /// 抛出字符串异常 /// /// 具体异常类型 /// 异常消息 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Oh(string errorMessage, params object[] args) where TException : class { return Oh(errorMessage, typeof(TException), args); } /// /// 抛出错误码异常 /// /// 错误码 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Oh(object errorCode, params object[] args) { var (ErrorCode, Message) = GetErrorCodeMessage(errorCode, args); var friendlyException = new AppFriendlyException(Message, errorCode) { ErrorCode = ErrorCode }; friendlyException.StatusCode = StatusCodes.Status400BadRequest; friendlyException.ValidationException = true; return friendlyException; } /// /// 抛出错误码异常 /// /// 错误码 /// 具体异常类型 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Oh(object errorCode, Type exceptionType, params object[] args) { var (ErrorCode, Message) = GetErrorCodeMessage(errorCode, args); return new AppFriendlyException(Message, errorCode, Activator.CreateInstance(exceptionType, new object[] { Message }) as Exception) { ErrorCode = ErrorCode }; } /// /// 抛出错误码异常 /// /// 具体异常类型 /// 错误码 /// String.Format 参数 /// 异常实例 public static AppFriendlyException Oh(object errorCode, params object[] args) where TException : class { return Oh(errorCode, typeof(TException), args); } /// /// 获取错误码消息 /// /// /// /// private static (object ErrorCode, string Message) GetErrorCodeMessage(object errorCode, params object[] args) { string errorMessage = ""; (errorCode, errorMessage) = HandleEnumErrorCode(errorCode); return (errorCode, errorMessage); } /// /// 处理枚举类型错误码 /// /// 错误码 /// private static (object ErrorCode, string Message) HandleEnumErrorCode(object errorCode) { string errorMessage = ""; // 获取类型 var errorType = errorCode.GetType(); // 判断是否是内置枚举类型,如果是解析特性 if (ErrorCodeTypes.Any(u => u == errorType)) { var fieldinfo = errorType.GetField(Enum.GetName(errorType, errorCode)); if (fieldinfo.IsDefined(typeof(ErrorCodeItemMetadataAttribute), true)) { var info = GetErrorCodeItemInformation(fieldinfo); errorCode = info.Key; errorMessage = info.Value; } } return (errorCode, errorMessage); } /// /// 获取错误代码信息 /// /// 字段对象 /// (object key, object value) private static (object Key, string Value) GetErrorCodeItemInformation(FieldInfo fieldInfo) { var errorCodeItemMetadata = fieldInfo.GetCustomAttribute(); return (errorCodeItemMetadata.ErrorCode ?? fieldInfo.Name, string.Format(errorCodeItemMetadata.ErrorMessage, errorCodeItemMetadata.Args)); } /// /// 获取错误代码类型 /// /// private static IEnumerable GetErrorCodeTypes() { var typesWithErrorCodeTypeAttribute = AppDomain.CurrentDomain .GetAssemblies() .SelectMany(assembly => assembly.GetTypes()) .Where(type => type.IsEnum && type.GetCustomAttribute() != null) .ToList(); return typesWithErrorCodeTypeAttribute; } } }