| | |
| | | using System.Net; |
| | | using Yw.Untity; |
| | | using Yw.Application; |
| | | using Yw.Dto; |
| | | |
| | | namespace IStation.WebApi |
| | | { |
| | | /// <summary> |
| | | /// 规范化RESTful风格返回值 |
| | | /// </summary> |
| | | [SuppressSniffer, UnifyModel(typeof(XnRestfulResult<>))] |
| | | [SuppressSniffer, UnifyModel(typeof(Result<>))] |
| | | public class XnRestfulResultProvider : IUnifyResultProvider |
| | | { |
| | | |
| | | /// <summary> |
| | | /// 异常 |
| | | /// </summary> |
| | |
| | | // 解析异常信息 |
| | | //var exceptionMetadata = UnifyContext.GetExceptionMetadata(context); |
| | | |
| | | XnRestfulResult<object> result = null; |
| | | Result<object> result = null; |
| | | |
| | | //furion异常 |
| | | if (context.Exception is AppFriendlyException friendException) |
| | | { |
| | | result = new XnRestfulResult<object> |
| | | result = new Result<object> |
| | | { |
| | | Code = (int)eResultCode.Alert, |
| | | Code = eResultCode.Alert, |
| | | Error = friendException.ErrorCode, |
| | | Message = friendException.ErrorMessage, |
| | | Data = null, |
| | |
| | | } |
| | | |
| | | //api异常 |
| | | else if (context.Exception is ApiException apiException) |
| | | else if (context.Exception is InternalException apiException) |
| | | { |
| | | result = new XnRestfulResult<object> |
| | | result = new Result<object> |
| | | { |
| | | Code = apiException.ResultCode, |
| | | Error = apiException.ErrorCode, |
| | |
| | | |
| | | else |
| | | { |
| | | result = new XnRestfulResult<object> |
| | | result = new Result<object> |
| | | { |
| | | Code = (int)eResultCode.Error, |
| | | Code = eResultCode.Error, |
| | | Error = "系统异常", |
| | | Message = context.Exception.StackTrace, |
| | | Data = null, |
| | |
| | | }; |
| | | } |
| | | |
| | | if (result.Code == (int)eResultCode.Error) |
| | | if (result.Code == eResultCode.Error) |
| | | { |
| | | LogHelper.Error($"错误码:{result.Error},错误信息:{result.Message}"); |
| | | Yw.LogHelper.Error($"错误码:{result.Error},错误信息:{result.Message}"); |
| | | } |
| | | |
| | | return new JsonResult(result); |
| | |
| | | return null; |
| | | } |
| | | |
| | | return new JsonResult(new XnRestfulResult<object> |
| | | return new JsonResult(new Result<object> |
| | | { |
| | | Code = (int)eResultCode.Success, |
| | | Code = eResultCode.Success, |
| | | Error = null, |
| | | Message = "请求成功", |
| | | Data = data, |
| | |
| | | /// </summary> |
| | | public IActionResult OnValidateFailed(ActionExecutingContext context, ValidationMetadata metadata) |
| | | { |
| | | return new JsonResult(new XnRestfulResult<object> |
| | | return new JsonResult(new Result<object> |
| | | { |
| | | Code = (int)eResultCode.Prompt, |
| | | Error = ErrorCodes.V001.ToString(), |
| | | Code = eResultCode.Prompt, |
| | | Error = InternalErrorCodes.V001.ToString(), |
| | | Message = metadata.Message, |
| | | Data = null, |
| | | Extras = UnifyContext.Take(), |
| | |
| | | |
| | | if (Enum.IsDefined(typeof(HttpStatusCode), (HttpStatusCode)statusCode)) |
| | | { |
| | | await context.Response.WriteAsJsonAsync(new XnRestfulResult<object> |
| | | await context.Response.WriteAsJsonAsync(new Result<object> |
| | | { |
| | | Code = (int)eResultCode.Confirm, |
| | | Error = ErrorCodes.A099.ToString(), |
| | | Code = eResultCode.Confirm, |
| | | Error = InternalErrorCodes.A099.ToString(), |
| | | Message = "权限验证失败", |
| | | Data = null, |
| | | Extras = UnifyContext.Take(), |