自定义返回级别
namespace Demo { /// <summary> /// 自定义返回级别 /// </summary> public enum ResultLevel : int { /// <summary> /// 正确 /// </summary> OK = 0, /// <summary> /// 警告 /// </summary> Warning = 1, /// <summary> /// 异常 /// </summary> Error = 2 } }View Code
自定义返回代码
namespace Demo { /// <summary> /// 自定义返回代码 /// </summary> public enum ResultCode : int { /// <summary> /// 正确 /// </summary> OK = 0, /// <summary> /// 用户名或密码错误 /// </summary> LoginFailed = 1, /// <summary> /// 身份认证失败 /// </summary> IdentityAuthFailed = 2, /// <summary> /// 数据模型已存在 /// </summary> ModelIsExist = 101, /// <summary> /// 数据模型不存在 /// </summary> ModelNoExist = 102, /// <summary> /// 数据模型错误 /// </summary> ModelError = 103, /// <summary> /// 文件校验失败 /// </summary> FileError = 104, /// <summary> /// MQTT回执超时 /// </summary> MqttRecriptTimeout = 105, /// <summary> /// MQTT回执结果失败 /// </summary> MqttRecriptResultFail = 106, /// <summary> /// HTTP请求失败 /// </summary> HttpRequestFail = 107, /// <summary> /// 请求内容类型错误 /// </summary> ContentTypeError = 996, /// <summary> /// 用户信息异常 /// </summary> UserInfoError = 997, /// <summary> /// 系统异常 /// </summary> LogicError = 998, /// <summary> /// 系统异常 /// </summary> OtherError = 999 } }View Code
自定义返回信息
namespace Demo { /// <summary> /// 自定义返回信息 /// </summary> public class ResultMsg { #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 public const string OK = "ok"; public const string LoginFailed = "用户名或密码错误"; public const string IdentityAuthFailed = "身份认证失败"; public const string ModelIsExist = "数据模型已存在"; public const string ModelNoExist = "数据模型不存在"; public const string ModelError = "数据模型错误"; public const string FileError = "文件校验失败"; public const string MqttRecriptTimeout = "等待MQTT回执超时"; public const string ContentTypeError = "请求内容类型错误"; public const string UserInfoError = "用户信息异常"; public const string LogicError = "业务逻辑异常"; public const string OtherError = "系统内部异常"; public const string HttpRequestFail = "HTTP请求失败:{0}"; #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 } }View Code
自定义返回结构
namespace Demo { /// <summary> /// 自定义返回结构 /// </summary> public class CustomResult<T> { /// <summary> /// 返回码 /// </summary> public string? Code { get; set; } /// <summary> /// 返回信息 /// </summary> public string? Msg { get; set; } = ResultMsg.OK; /// <summary> /// 返回数据 /// </summary> public T? Data { get; set; } /// <summary> /// 构造函数 /// </summary> public CustomResult() { Code = ((int)ResultLevel.OK).ToString() + ((int)ResultCode.OK).ToString("D3"); } /// <summary> /// 构造函数 /// </summary> /// <param name="t"></param> public CustomResult(T? t) { if (t is CustomException cex) { Code = ((int)cex.Level).ToString() + ((int)cex.Code).ToString("D3"); Msg = cex.Message; } else if (t is Exception) { Code = ((int)ResultLevel.Error).ToString() + ((int)ResultCode.OtherError).ToString("D3"); Msg = ResultMsg.OtherError; } else { Code = ((int)ResultLevel.OK).ToString() + ((int)ResultCode.OK).ToString("D3"); Data = t; } } /// <summary> /// 构造函数 /// </summary> /// <param name="level"></param> /// <param name="code"></param> /// <param name="t"></param> public CustomResult(ResultLevel level, ResultCode code, T t) { Code = ((int)level).ToString() + ((int)code).ToString("D3"); Data = t; } } }View Code
自定义异常类
namespace Demo { /// <summary> /// 自定义异常类 /// </summary> [Serializable] public class CustomException : Exception { /// <summary> /// 错误级别 /// </summary> public ResultLevel Level { get; set; } = ResultLevel.Error; /// <summary> /// 错误码 /// </summary> public ResultCode Code { get; set; } = ResultCode.OtherError; /// <summary> /// 构造函数 /// </summary> public CustomException() { } /// <summary> /// 构造函数 /// </summary> /// <param name="message"></param> public CustomException(string? message) : base(message) { } /// <summary> /// 构造函数 /// </summary> /// <param name="errLevel"></param> /// <param name="errCode"></param> /// <param name="message"></param> public CustomException(ResultLevel errLevel, ResultCode errCode, string? message) : base(message) { Level = errLevel; Code = errCode; } /// <summary> /// 构造函数 /// </summary> /// <param name="errLevel"></param> /// <param name="errCode"></param> public CustomException(ResultLevel errLevel, ResultCode errCode) : base() { Level = errLevel; Code = errCode; } } }View Code
自定义异常过滤器(控制器全局异常过滤器)
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Filters; namespace Demo { /// <summary> /// 自定义异常过滤器(控制器异常) /// </summary> public class CustomExceptionFilterAttribute : ExceptionFilterAttribute { /// <summary> /// 日志 /// </summary> private readonly ILogger _logger; /// <summary> /// 构造函数 /// </summary> /// <param name="logger"></param> public CustomExceptionFilterAttribute(ILogger<CustomExceptionFilterAttribute> logger) { _logger = logger; } /// <summary> /// 发生异常事件 /// </summary> /// <param name="context"></param> public override void OnException(ExceptionContext context) { // 自定义异常 if (context.Exception is CustomException ex) { _logger.LogError(ex, ResultMsg.LogicError); var result = new CustomResult<CustomException>(ex); context.Result = new BadRequestObjectResult(result); } // 系统异常 else { _logger.LogError(context.Exception, ResultMsg.OtherError); var result = new CustomResult<Exception>(context.Exception); context.Result = new ObjectResult(result) { StatusCode = StatusCodes.Status500InternalServerError }; } // 标记异常已处理 context.ExceptionHandled = true; } /// <summary> /// 发生异常事件 /// </summary> /// <param name="context"></param> /// <returns></returns> public override Task OnExceptionAsync(ExceptionContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } OnException(context); return Task.CompletedTask; } } }View Code
使用方法
// 添加控制器 builder.Services.AddControllers(options => { // 自定义异常过滤器(仅对控制器级别的错误进行处理) options.Filters.Add(typeof(CustomExceptionFilterAttribute)); })View Code
翻译
搜索
复制
标签:WebAPI,Code,const,string,自定义,C#,int,NET6,public From: https://www.cnblogs.com/smartnn/p/18110017