Skip to content

Commit

Permalink
NHttpRequest优化:BadHttpRequestException异常时不输出调用堆栈
Browse files Browse the repository at this point in the history
  • Loading branch information
fish-li committed Dec 28, 2023
1 parent 7c61a78 commit 9bba313
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/ClownFish.Email/ClownFish.Email.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.Email</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.ImClients/ClownFish.ImClients.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.ImClients</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.Office/ClownFish.Office.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.Office</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.Rabbit/ClownFish.Rabbit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.Rabbit</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.Redis/ClownFish.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.NRedis</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.Tracing/ClownFish.Tracing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.Tracing</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<OutputType>Library</OutputType>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.Web/ClownFish.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish.Web</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
4 changes: 2 additions & 2 deletions src/ClownFish.net/ClownFish.net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<TargetFrameworks>net48;net6.0;net7.0;net8.0</TargetFrameworks>
<RootNamespace>ClownFish</RootNamespace>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.1221.1</Version>
<FileVersion>8.0.1221.1</FileVersion>
<Version>8.0.1228.1</Version>
<FileVersion>8.0.1228.1</FileVersion>
<SatelliteResourceLanguages>xx</SatelliteResourceLanguages>
<OutputPath>bin</OutputPath>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
24 changes: 16 additions & 8 deletions src/ClownFish.net/Http/Pipleline/NHttpRequest2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public async Task<string> GetBodyTextAsync()
// 2, Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Request body too large. The max request body size is xxxxxxxxxx bytes.



private void ShowReadBodyException(string method, Exception ex)
{
if( ex.GetType().Name == "BadHttpRequestException" ) {
//这种异常没法解决,显示堆栈也任何作用,反而把 Console 搞得很乱
Console2.Warnning(method + " ERROR: " + ex.Message);
}
else {
Console2.Warnning(method + " ERROR: " + ex.ToString());
}
}

/// <summary>
/// 按 字符串 形式读取请求体内容。
/// 【##### 此方法不做结果缓存,因此不要多次调用 #####】
Expand All @@ -68,8 +80,7 @@ public virtual string ReadBodyAsText()
return reader.ReadAllText(encoding);
}
catch( Exception ex ) {
Console2.Warnning("ReadBodyAsText ERROR: " + ex.ToString());
// 吃异常的原因请参考上面注释
ShowReadBodyException(nameof(ReadBodyAsText), ex);
return string.Empty;
}
}
Expand All @@ -96,8 +107,7 @@ public virtual async Task<string> ReadBodyAsTextAsync()
return await reader.ReadAllTextAsync(encoding);
}
catch( Exception ex ) {
Console2.Warnning("ReadBodyAsTextAsync ERROR: " + ex.ToString());
// 吃异常的原因请参考上面注释
ShowReadBodyException(nameof(ReadBodyAsTextAsync), ex);
return string.Empty;
}
}
Expand All @@ -117,8 +127,7 @@ public virtual byte[] ReadBodyAsBytes()
return this.InputStream.ToArray();
}
catch( Exception ex ) {
Console2.Warnning("ReadBodyAsBytes ERROR: " + ex.ToString());
// 吃异常的原因请参考上面注释
ShowReadBodyException(nameof(ReadBodyAsBytes), ex);
return Empty.Array<byte>();
}
}
Expand All @@ -138,8 +147,7 @@ public virtual async Task<byte[]> ReadBodyAsBytesAsync()
return await this.InputStream.ToArrayAsync();
}
catch( Exception ex ) {
Console2.Warnning("ReadBodyAsBytesAsync ERROR: " + ex.ToString());
// 吃异常的原因请参考上面注释
ShowReadBodyException(nameof(ReadBodyAsBytesAsync), ex);
return Empty.Array<byte>();
}
}
Expand Down

0 comments on commit 9bba313

Please sign in to comment.