为api异常增加Id,可以通过ThrowApiException一个重载方法设置Id的值,否则会自动生成

This commit is contained in:
falcon 2022-10-08 16:50:30 +08:00
parent da07c27221
commit 6d2023adfe
3 changed files with 28 additions and 1 deletions

View File

@ -113,5 +113,24 @@ namespace Falcon.SugarApi.ApiDefinistions
/// <exception cref="ApiException">api异常</exception>
protected virtual void ThrowApiException(string msg)
=> throw new ApiException(msg);
/// <summary>
/// 抛出api异常
/// </summary>
/// <param name="innException">内部异常</param>
/// <exception cref="ApiException">api异常</exception>
protected virtual void ThrowApiException(Exception innException)
=> throw new ApiException(innException.Message, innException);
/// <summary>
/// 抛出api异常
/// </summary>
/// <param name="msg">异常消息</param>
/// <param name="innException">内部异常</param>
/// <param name="exAction">异常处理方法</param>
protected virtual void ThrowApiException(string msg, Exception innException, Action<ApiException> exAction) {
var ex = new ApiException(msg, innException);
exAction(ex);
throw ex;
}
}
}

View File

@ -7,6 +7,11 @@ namespace Falcon.SugarApi.ApiDefinistions
/// </summary>
public class ApiException : Exception
{
/// <summary>
/// 异常信息Id
/// </summary>
public Guid? Id { get; set; }
/// <summary>
/// 通过异常信息创建异常
/// </summary>

View File

@ -44,7 +44,10 @@ namespace Falcon.SugarApi.ApiDefinistions
CreateDateTime = now,
Message = exception.Message,
};
if (exception is ApiException) {
if (exception is ApiException ae) {
if (ae.Id.HasValue) {
model.Id = ae.Id.Value.ToString();
}
context.Result = new ObjectResult(model) { StatusCode = this.StatusCode, };
context.ExceptionHandled = true;
}