NetCore 统一格式(错误异常)

使用方式:
throw new ResponseException("未授权,操作失败");

 

异常输出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Micro.Core.Utility
{
    [Serializable]
    public class ResponseException : ApplicationException
    {
        /// <summary>
        /// 状态码
        /// </summary>
        private readonly int _code;

        /// <summary>
        /// 错误码,当为0时,代表正常
        /// </summary>
        private readonly ResponseCode _errorCode;
        /// <summary>
        /// 构造函数
        /// </summary>
        public ResponseException() : base("服务器繁忙,请稍后再试!")
        {
            _errorCode = ResponseCode.Exception;
            _code = 400;
        }

        public ResponseException(string? message = "服务器繁忙,请稍后再试!", ResponseCode errorCode = ResponseCode.Exception, int code = 400) : base(message)
        {
            this._errorCode = errorCode;
            _code = code;
        }

        /// <summary>
        /// 获取状态码
        /// </summary>
        public int GetCode()
        {
            return _code;
        }

        public ResponseCode GetErrorCode()
        {
            return _errorCode;
        }
    }
}

 

posted @ 2024-01-22 10:37  microsoft-zhcn  阅读(5)  评论(0编辑  收藏  举报