Skip to content

Commit

Permalink
fix: support get and set of exception fields
Browse files Browse the repository at this point in the history
  • Loading branch information
飞澋 authored and PanPanZou committed Jun 3, 2024
1 parent 329e6d0 commit 93a11b3
Show file tree
Hide file tree
Showing 10 changed files with 907 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 2.1.811
dotnet-version: 3.1.426
- name: install altcover
run: dotnet tool install --global altcover.visualizer --version 8.6.14
- name: Install dependencies
Expand Down
16 changes: 8 additions & 8 deletions Tea/TeaCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public static TeaResponse DoAction(TeaRequest request, Dictionary<string, object

try
{
HttpClient httpClient = HttpClientUtils.GetOrAddHttpClient(request.Protocol,uri.Host, uri.Port, runtimeOptions);
HttpClient httpClient = HttpClientUtils.GetOrAddHttpClient(request.Protocol, uri.Host, uri.Port, runtimeOptions);
HttpResponseMessage response = httpClient.SendAsync(req, new CancellationTokenSource(timeout).Token).Result;
return new TeaResponse(response);
}
catch (System.Threading.Tasks.TaskCanceledException)
catch (TaskCanceledException)
{
throw new WebException("operation is timeout");
}
Expand All @@ -105,15 +105,15 @@ public static async Task<TeaResponse> DoActionAsync(TeaRequest request, Dictiona
HttpResponseMessage response = await httpClient.SendAsync(req, new CancellationTokenSource(timeout).Token);
return new TeaResponse(response);
}
catch (System.Threading.Tasks.TaskCanceledException)
catch (TaskCanceledException)
{
throw new WebException("operation is timeout");
}
}

public static string GetResponseBody(TeaResponse response)
{
using(var ms = new MemoryStream())
using (var ms = new MemoryStream())
{
var buffer = new byte[bufferLength];
var stream = response.Body;
Expand Down Expand Up @@ -163,16 +163,16 @@ public static Dictionary<string, string> ConvertHeaders(HttpResponseHeaders head

public static bool AllowRetry(IDictionary dict, int retryTimes, long now)
{
if(retryTimes == 0)
if (retryTimes == 0)
{
return true;
}

if(!dict.Get("retryable").ToSafeBool(false))
if (!dict.Get("retryable").ToSafeBool(false))
{
return false;
}

int retry;
if (dict == null)
{
Expand Down Expand Up @@ -260,7 +260,7 @@ internal static string PercentEncode(string value)
}
else
{
stringBuilder.Append("%").Append(string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int) c));
stringBuilder.Append("%").Append(string.Format(CultureInfo.InvariantCulture, "{0:X2}", (int)c));
}
}

Expand Down
73 changes: 18 additions & 55 deletions Tea/TeaException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,38 @@ namespace Tea
{
public class TeaException : Exception
{
private string code;
private string message;
private Dictionary<string, object> data;
private int statusCode;
private string description;
private Dictionary<string, object> accessDeniedDetail;

public string Code
{
get
{
return code;
}
}

public override string Message
{
get
{
return message;
}
}
public string Code { get; set; }
public new string Message { get; set; }
public new Dictionary<string, object> Data { get; set; }
public int StatusCode { get; set; }
public string Description { get; set; }
public Dictionary<string, object> AccessDeniedDetail { get; set; }

public Dictionary<string, object> DataResult
{
get
{
return data;
}
}

public int StatusCode
{
get
{
return statusCode;
}
}


public string Description
{
get
{
return description;
return Data;
}
}

public Dictionary<string, object> AccessDeniedDetail
public TeaException()
{
get
{
return accessDeniedDetail;
}
}

public TeaException(IDictionary dict)
{
Dictionary<string, object> dicObj = dict.Keys.Cast<string>().ToDictionary(key => key, key => dict[key]);
code = DictUtils.GetDicValue(dicObj, "code").ToSafeString();
message = DictUtils.GetDicValue(dicObj, "message").ToSafeString();
description = DictUtils.GetDicValue(dicObj, "description").ToSafeString();
Code = DictUtils.GetDicValue(dicObj, "code").ToSafeString();
Message = DictUtils.GetDicValue(dicObj, "message").ToSafeString();
Description = DictUtils.GetDicValue(dicObj, "description").ToSafeString();
object obj = DictUtils.GetDicValue(dicObj, "accessDeniedDetail");
if (obj != null)
{
if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
{
IDictionary dicDetail = (IDictionary) obj;
accessDeniedDetail = dicDetail.Keys.Cast<string>().ToDictionary(key => key, key => dicDetail[key]);
IDictionary dicDetail = (IDictionary)obj;
AccessDeniedDetail = dicDetail.Keys.Cast<string>().ToDictionary(key => key, key => dicDetail[key]);
}
}
obj = DictUtils.GetDicValue(dicObj, "data");
Expand All @@ -88,11 +51,11 @@ public TeaException(IDictionary dict)
}
if (typeof(IDictionary).IsAssignableFrom(obj.GetType()))
{
IDictionary dicData = (IDictionary) obj;
data = dicData.Keys.Cast<string>().ToDictionary(key => key, key => dicData[key]);
if (DictUtils.GetDicValue(data, "statusCode") != null)
IDictionary dicData = (IDictionary)obj;
Data = dicData.Keys.Cast<string>().ToDictionary(key => key, key => dicData[key]);
if (DictUtils.GetDicValue(Data, "statusCode") != null)
{
statusCode = int.Parse(DictUtils.GetDicValue(data, "statusCode").ToSafeString());
StatusCode = int.Parse(DictUtils.GetDicValue(Data, "statusCode").ToSafeString());
}
return;
}
Expand All @@ -105,7 +68,7 @@ public TeaException(IDictionary dict)
PropertyInfo p = properties[i];
filedsDict.Add(p.Name, p.GetValue(obj));
}
data = filedsDict;
Data = filedsDict;
}
}
}
3 changes: 2 additions & 1 deletion Tea/TeaResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ public Stream Body
}
}


public TeaResponse(HttpResponseMessage response)
{
if (response != null)
{
StatusCode = (int) response.StatusCode;
StatusCode = (int)response.StatusCode;
StatusMessage = "";
Headers = TeaCore.ConvertHeaders(response.Headers);
_responseAsync = response;
Expand Down
Loading

0 comments on commit 93a11b3

Please sign in to comment.