Skip to content

Commit

Permalink
[feat] 解析Http400响应中的错误内容,便于分析接口的BadRequest错误原因
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 7, 2023
1 parent 82b044d commit 39eef57
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
7 changes: 5 additions & 2 deletions NewLife.Core/Log/ITracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,12 @@ protected virtual void ProcessSpans(ISpanBuilder[] builders)
WriteLog("Tracer[{0}] Total={1:n0} Errors={2:n0} Speed={3:n2}tps Cost={4:n0}ms MaxCost={5:n0}ms MinCost={6:n0}ms", bd.Name, bd.Total, bd.Errors, speed, bd.Cost / bd.Total, bd.MaxCost, bd.MinCost);

#if DEBUG
foreach (var span in bd.Samples)
if (bd.Samples != null)
{
WriteLog("Span Id={0} ParentId={1} TraceId={2} Tag={3} Error={4}", span.Id, span.ParentId, span.TraceId, span.Tag, span.Error);
foreach (var span in bd.Samples)
{
WriteLog("Span Id={0} ParentId={1} TraceId={2} Tag={3} Error={4}", span.Id, span.ParentId, span.TraceId, span.Tag, span.Error);
}
}
#endif
}
Expand Down
3 changes: 1 addition & 2 deletions NewLife.Core/Net/TcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ internal override Boolean OnReceiveAsync(SocketAsyncEventArgs se)
/// <param name="ar"></param>
private void OnEndRead(IAsyncResult ar)
{
var se = ar.AsyncState as SocketAsyncEventArgs;
Int32 bytes;
try
{
Expand All @@ -375,7 +374,7 @@ private void OnEndRead(IAsyncResult ar)
return;
}

if (se != null) ProcessEvent(se, bytes, true);
if (ar.AsyncState is SocketAsyncEventArgs se) ProcessEvent(se, bytes, true);
}

private Int32 _empty;
Expand Down
12 changes: 12 additions & 0 deletions NewLife.Core/Remoting/ApiHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,18 @@ public static HttpContent BuildContent(Packet pk)
if (response.StatusCode >= HttpStatusCode.BadRequest)
{
var msg = buf?.ToStr().Trim('\"');
// 400响应可能包含错误信息
if (!msg.IsNullOrEmpty() && msg.StartsWith("{") && msg.EndsWith("}"))
{
var dic = JsonParser.Decode(msg);
if (dic != null)
{
var msg2 = "";
if (dic.TryGetValue("title", out var v)) msg2 = v + "";
if (dic.TryGetValue("errors", out v)) msg2 += v?.ToJson();
if (!msg2.IsNullOrEmpty()) msg = msg2.Trim();
}
}
if (msg.IsNullOrEmpty()) msg = response.ReasonPhrase;
if (msg.IsNullOrEmpty()) msg = response.StatusCode + "";
throw new ApiException((Int32)response.StatusCode, msg);
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Core/Remoting/ApiHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,8 @@ protected virtual async Task<HttpResponseMessage> SendOnServiceAsync(HttpRequest

if (filter != null) await filter.OnResponse(client, response, this, cancellationToken);

// 业务层只会返回200 OK
response.EnsureSuccessStatusCode();
//// 业务层只会返回200 OK
//response.EnsureSuccessStatusCode();

return response;
}
Expand Down

0 comments on commit 39eef57

Please sign in to comment.