/// <summary> /// 构造函数 /// </summary> /// <param name="data"></param> /// <param name="status"></param> /// <param name="msg"></param> /// <param name="ex"></param> public ApiResponse(T?data, int status = 0, string msg = "Ok", Exception?ex = null) { Status = status; Msg = msg; Data = data; if (status == 0 && data != null) { var type = typeof(T) !; IsList = data is IEnumerable; if (IsList) { var first = type.GenericTypeArguments.First(); EntityName = first.Name; EntityFullName = first.FullName; } else { EntityName = type.Name; EntityFullName = type.FullName; } } if (status == 0) { return; } if (ex == null) { return; } ErrorDetail = BadRequestMessage.CreateMessage(ex, out var errMsg); Msg = errMsg; }
/// <summary> /// 创建错误对象 /// </summary> /// <param name="e"></param> /// <param name="message"></param> /// <returns></returns> public static BadRequestMessage CreateMessage(Exception e, out string message) { BadRequestMessage bm; if (e is ValidationException ev) { bm = new BadRequestMessage { MessageType = EnumBadRequestMessageType.Validate, Message = ev.Message }; message = "校验错误,查看详细信息。"; } else { bm = new BadRequestMessage { MessageType = EnumBadRequestMessageType.Other, Message = e.Message }; message = e.Message; } return(bm); }