Skip to content

Commit

Permalink
fix: 修改json序列化错误处理逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukari316 committed Sep 22, 2023
1 parent 9b0458a commit 7c6cbd8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
17 changes: 11 additions & 6 deletions Sora/Net/SoraWebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ internal SoraWebsocketClient(ClientConfig config, Action<Exception> crashAction
//全局异常事件
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
if (crashAction == null)
Helper.FriendlyException(args);
else
crashAction(args.ExceptionObject as Exception);
Log.UnhandledExceptionLog(args);
crashAction(args.ExceptionObject as Exception);
};
_isReady = true;
}
Expand Down Expand Up @@ -140,11 +138,18 @@ public async ValueTask StartService()
ErrorReconnectTimeout = Config.ReconnectTimeOut
};
//消息接收事件
_subClientMessageReceived = Client.MessageReceived.Subscribe(msg => Task.Run(() =>
_subClientMessageReceived = Client.MessageReceived.Subscribe(msg => Task.Run(async () =>
{
if (_disposed || string.IsNullOrEmpty(msg.Text))
return;
Event.Adapter(JObject.Parse(msg.Text), ServiceId);
try
{
await Event.Adapter(JObject.Parse(msg.Text), ServiceId);
}
catch (Exception e)
{
Helper.FriendlyException(e);
}
}));
//连接断开事件
_subClientDisconnectionHappened = Client.DisconnectionHappened.Subscribe(info => Task.Run(() =>
Expand Down
17 changes: 11 additions & 6 deletions Sora/Net/SoraWebsocketServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,8 @@ internal SoraWebsocketServer(ServerConfig config, Action<Exception> crashAction
//全局异常事件
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
{
if (crashAction == null)
Helper.FriendlyException(args);
else
crashAction(args.ExceptionObject as Exception);
Log.UnhandledExceptionLog(args);
crashAction(args.ExceptionObject as Exception);
};
_isReady = true;
}
Expand Down Expand Up @@ -193,11 +191,18 @@ private void SocketEvent(IWebSocketConnection socket)
Log.Info("Sora", $"客户端连接被关闭[{socket.ConnectionInfo.ClientIpAddress}:{socket.ConnectionInfo.ClientPort}]");
};
//上报接收
socket.OnMessage = message => Task.Run(() =>
socket.OnMessage = message => Task.Run(async () =>
{
if (_disposed || !_isRunning)
return;
Event.Adapter(JObject.Parse(message), socket.ConnectionInfo.Id);
try
{
await Event.Adapter(JObject.Parse(message), socket.ConnectionInfo.Id);
}
catch (Exception e)
{
Helper.FriendlyException(e);
}
});
}

Expand Down
22 changes: 11 additions & 11 deletions Sora/OnebotAdapter/EventAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public delegate ValueTask EventAsyncCallBackHandler<in TEventArgs>(string eventT
/// </summary>
/// <param name="messageJson">消息json对象</param>
/// <param name="connection">客户端链接接口</param>
internal void Adapter(JObject messageJson, Guid connection)
internal async ValueTask Adapter(JObject messageJson, Guid connection)
{
if (!ServiceRecord.Exists(ServiceId))
{
Expand All @@ -222,19 +222,19 @@ internal void Adapter(JObject messageJson, Guid connection)
{
//元事件类型
case "meta_event":
MetaAdapter(messageJson, connection);
await MetaAdapter(messageJson, connection);
break;
case "message":
MessageAdapter(messageJson, connection);
await MessageAdapter(messageJson, connection);
break;
case "request":
RequestAdapter(messageJson, connection);
await RequestAdapter(messageJson, connection);
break;
case "notice":
NoticeAdapter(messageJson, connection);
await NoticeAdapter(messageJson, connection);
break;
case "message_sent":
SelfMessageAdapter(messageJson, connection);
await SelfMessageAdapter(messageJson, connection);
break;
default:
//尝试从响应中获取标识符
Expand All @@ -258,7 +258,7 @@ internal void Adapter(JObject messageJson, Guid connection)
/// </summary>
/// <param name="messageJson">消息</param>
/// <param name="connection">连接GUID</param>
private async void MetaAdapter(JObject messageJson, Guid connection)
private async ValueTask MetaAdapter(JObject messageJson, Guid connection)
{
switch (TryGetJsonValue(messageJson, "meta_event_type"))
{
Expand Down Expand Up @@ -336,7 +336,7 @@ await OnClientConnect("Meta Event",
/// </summary>
/// <param name="messageJson">消息</param>
/// <param name="connection">连接GUID</param>
private async void MessageAdapter(JObject messageJson, Guid connection)
private async ValueTask MessageAdapter(JObject messageJson, Guid connection)
{
switch (TryGetJsonValue(messageJson, "message_type"))
{
Expand Down Expand Up @@ -409,7 +409,7 @@ private async void MessageAdapter(JObject messageJson, Guid connection)
/// </summary>
/// <param name="messageJson">消息</param>
/// <param name="connection">连接GUID</param>
private async void SelfMessageAdapter(JObject messageJson, Guid connection)
private async ValueTask SelfMessageAdapter(JObject messageJson, Guid connection)
{
switch (TryGetJsonValue(messageJson, "message_type"))
{
Expand Down Expand Up @@ -457,7 +457,7 @@ await OnSelfPrivateMessage("Message",
/// </summary>
/// <param name="messageJson">消息</param>
/// <param name="connection">连接GUID</param>
private async void RequestAdapter(JObject messageJson, Guid connection)
private async ValueTask RequestAdapter(JObject messageJson, Guid connection)
{
switch (TryGetJsonValue(messageJson, "request_type"))
{
Expand Down Expand Up @@ -518,7 +518,7 @@ await OnGroupRequest("Request",
/// </summary>
/// <param name="messageJson">消息</param>
/// <param name="connection">连接GUID</param>
private async void NoticeAdapter(JObject messageJson, Guid connection)
private async ValueTask NoticeAdapter(JObject messageJson, Guid connection)
{
switch (TryGetJsonValue(messageJson, "notice_type"))
{
Expand Down
11 changes: 6 additions & 5 deletions Sora/Util/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ public static class Helper
/// <summary>
/// 友好的崩溃提示(x)
/// </summary>
[Reviewed("nidbCN", "2021-03-24 19:31")]
internal static void FriendlyException(UnhandledExceptionEventArgs args)
internal static void FriendlyException(Exception e)
{
Exception e = args.ExceptionObject as Exception;

if (e is JsonSerializationException)
{
Log.Error("Sora", "Json反序列化时出现错误,可能是go-cqhttp配置出现问题。请把go-cqhttp配置中的post_message_format从string改为array。");
return;
}

Log.UnhandledExceptionLog(args);
Log.Error(e, "Sora", "发生未知错误");
throw e;
}

#endregion
Expand Down

0 comments on commit 7c6cbd8

Please sign in to comment.