添加dto模式的demo测试
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.Common.Exceptions;
|
||||
|
||||
[Serializable]
|
||||
public class BusinessException : Exception,
|
||||
IHasErrorCode,
|
||||
IHasErrorDetails,
|
||||
IHasLogLevel
|
||||
{
|
||||
public ResultCodeEnum Code { get; set; }
|
||||
|
||||
public string Details { get; set; }
|
||||
|
||||
public LogLevel LogLevel { get; set; }
|
||||
|
||||
public BusinessException(
|
||||
ResultCodeEnum code = ResultCodeEnum.NotSuccess,
|
||||
string message = null,
|
||||
string details = null,
|
||||
Exception innerException = null,
|
||||
LogLevel logLevel = LogLevel.Warning)
|
||||
: base(message, innerException)
|
||||
{
|
||||
Code = code;
|
||||
Details = details;
|
||||
LogLevel = logLevel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 序列化参数的构造函数
|
||||
/// </summary>
|
||||
public BusinessException(SerializationInfo serializationInfo, StreamingContext context)
|
||||
: base(serializationInfo, context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public BusinessException WithData(string name, object value)
|
||||
{
|
||||
Data[name] = value;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using System.Runtime.ExceptionServices;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.Common.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="Exception"/><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ<EFBFBD><D5B9><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
public static class ExceptionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// ʹ<><CAB9> <see cref="ExceptionDispatchInfo.Capture"/> <20>ٴ<EFBFBD><D9B4>׳<EFBFBD><D7B3>쳣
|
||||
/// </summary>
|
||||
/// <param name="exception">Exception to be re-thrown</param>
|
||||
public static void ReThrow(this Exception exception)
|
||||
{
|
||||
ExceptionDispatchInfo.Capture(exception).Throw();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD>쳣<EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD>ȼ<EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="exception"></param>
|
||||
/// <param name="defaultLevel"></param>
|
||||
/// <returns></returns>
|
||||
public static LogLevel GetLogLevel(this Exception exception, LogLevel defaultLevel = LogLevel.Error)
|
||||
{
|
||||
return (exception as IHasLogLevel)?.LogLevel ?? defaultLevel;
|
||||
}
|
||||
/// <summary>
|
||||
/// <20><>ȡ<EFBFBD>쳣<EFBFBD>е<EFBFBD><D0B5><EFBFBD>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
/// </summary>
|
||||
/// <param name="exception"></param>
|
||||
/// <param name="defaultLevel"></param>
|
||||
/// <returns></returns>
|
||||
public static ResultCodeEnum GetLogErrorCode(this Exception exception, ResultCodeEnum defaultCode = ResultCodeEnum.NotSuccess)
|
||||
{
|
||||
return (exception as IHasErrorCode)?.Code ?? defaultCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.Common.Exceptions;
|
||||
|
||||
public interface IHasErrorCode
|
||||
{
|
||||
ResultCodeEnum Code { get; }
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Yi.Framework.Common.Exceptions;
|
||||
|
||||
public interface IHasErrorDetails
|
||||
{
|
||||
string Details { get; }
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Yi.Framework.Common.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD><EFBFBD><see cref="LogLevel"/> <20><><EFBFBD>ԵĽӿ<C4BD>
|
||||
/// </summary>
|
||||
public interface IHasLogLevel
|
||||
{
|
||||
/// <summary>
|
||||
/// Log severity.
|
||||
/// </summary>
|
||||
LogLevel LogLevel { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Yi.Framework.Common.Enum;
|
||||
|
||||
namespace Yi.Framework.Common.Exceptions;
|
||||
|
||||
/// <summary>
|
||||
/// <20>û<EFBFBD><C3BB>Ѻ<EFBFBD><D1BA>쳣
|
||||
/// </summary>
|
||||
[Serializable]
|
||||
public class UserFriendlyException : BusinessException
|
||||
{
|
||||
public UserFriendlyException(
|
||||
string message,
|
||||
ResultCodeEnum code = ResultCodeEnum.NotSuccess,
|
||||
string details = null,
|
||||
Exception innerException = null,
|
||||
LogLevel logLevel = LogLevel.Warning)
|
||||
: base(
|
||||
code,
|
||||
message,
|
||||
details,
|
||||
innerException,
|
||||
logLevel)
|
||||
{
|
||||
Details = details;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9>캯<EFBFBD><ECBAAF>
|
||||
/// </summary>
|
||||
public UserFriendlyException(SerializationInfo serializationInfo, StreamingContext context)
|
||||
: base(serializationInfo, context)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user