catch all exception in http req/rsp

This commit is contained in:
DismissedLight
2024-07-17 16:57:23 +08:00
parent fbe6abc63a
commit 707fc67e51

View File

@@ -1,9 +1,9 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
using Snap.Hutao.Core;
using Snap.Hutao.Service.Notification;
using Snap.Hutao.Web.Hutao.Response;
using Snap.Hutao.Web.Response;
using System.IO;
using System.Net;
using System.Net.Http;
@@ -45,6 +45,7 @@ internal static class HttpRequestMessageBuilderExtension
{
if (TryHandleHttp502HutaoResponseSpecialCase(ex, out TResult? result))
{
showInfo = false;
return result;
}
@@ -71,6 +72,11 @@ internal static class HttpRequestMessageBuilderExtension
ProcessException(messageBuilder, ex);
logger.LogWarning(ex, RequestErrorMessage, builder.RequestUri);
}
catch (Exception ex)
{
ProcessException(messageBuilder, ex);
logger.LogWarning(ex, RequestErrorMessage, builder.RequestUri);
}
finally
{
if (showInfo)
@@ -148,34 +154,36 @@ internal static class HttpRequestMessageBuilderExtension
.AppendLine($"{nameof(HttpRequestException)}: Status Code: {hre.StatusCode} Error: {hre.HttpRequestError}")
.AppendLine(hre.Message);
}
if (exception is IOException ioe)
else if (exception is IOException ioe)
{
builder
.AppendLine($"{nameof(IOException)}: 0x{ioe.HResult:X8}")
.AppendLine(ioe.Message);
}
if (exception is JsonException je)
else if (exception is JsonException je)
{
builder
.AppendLine($"{nameof(JsonException)}: Path: {je.Path} at Line: {je.LineNumber} Position: {je.BytePositionInLine}")
.AppendLine(je.Message);
}
if (exception is HttpContentSerializationException hcse)
else if (exception is HttpContentSerializationException hcse)
{
builder
.AppendLine($"{nameof(HttpContentSerializationException)}:")
.AppendLine(hcse.Message);
}
if (exception is SocketException se)
else if (exception is SocketException se)
{
builder
.AppendLine($"{nameof(SocketException)}: Error: {se.SocketErrorCode}")
.AppendLine(se.Message);
}
else
{
builder
.AppendLine($"{TypeNameHelper.GetTypeDisplayName(exception, false)}:")
.AppendLine(exception.Message);
}
if (exception.InnerException is { } inner)
{