/// <summary> /// api异常 /// </summary> /// <param name="message">消息</param> /// <param name="statusCode">状态码</param> /// <param name="response">响应数据</param> /// <param name="headers">头</param> /// <param name="innerException"></param> public ApiException( string message, int statusCode, string response, IReadOnlyDictionary <string, IEnumerable <string> > headers, Exception innerException) : base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + response.Substring(0, response.Length >= 512 ? 512 : response.Length), innerException) { StatusCode = statusCode; Response = response; Headers = headers; if (statusCode == 400) { string[] msg = null; var bad = JsonConvert.DeserializeObject <RequestBad>(Response); if (bad != null) { if (bad.Errors != null) { foreach (var dic in bad.Errors) { msg = dic.Value; break; } } } if (msg != null) { AjaxInfo = new AjaxInfo { IsSuccess = false, Code = -1, Message = msg.Length > 0 ? msg[0] : "" } } ; } else { try { AjaxInfo = JsonConvert.DeserializeObject <AjaxInfo>(Response); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// 获取ajaxinfo /// </summary> /// <param name="task">任务</param> /// <returns></returns> public static AjaxInfo GetAjaxInfo(this Task task) { AjaxInfo ajaxInfo = null; if (task.Status != TaskStatus.RanToCompletion) { if (task.Status == TaskStatus.Canceled) { ajaxInfo = new AjaxInfo { IsSuccess = false, Message = "连接超时!" } } ; else { if (task.Exception.IsNull()) { ajaxInfo = new AjaxInfo { IsSuccess = false, Message = "找不到错误!" } } ; else { if (task.Exception.InnerException != null) { ajaxInfo = FindInnerException(task.Exception.InnerException); } else { ajaxInfo = new AjaxInfo { IsSuccess = false, Message = task.Exception.Message } }; } } } return(ajaxInfo); } }