Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid program crashes due to json deserialization failure #77

Merged
1 commit merged into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions Lagrange.OneBot/Core/Operation/OperationService.cs
Original file line number Diff line number Diff line change
@@ -21,47 +21,53 @@ public OperationService(BotContext bot, ILogger<OperationService> logger, LiteDa
{
_bot = bot;
_logger = logger;

_operations = new Dictionary<string, Type>();
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
var attribute = type.GetCustomAttribute<OperationAttribute>();
if (attribute != null) _operations[attribute.Api] = type;
}

var service = new ServiceCollection();
service.AddSingleton(context);
service.AddSingleton(logger);

foreach (var (_, type) in _operations) service.AddScoped(type);
_service = service.BuildServiceProvider();
}

public async Task<OneBotResult?> HandleOperation(MsgRecvEventArgs e)
{
if (JsonSerializer.Deserialize<OneBotAction>(e.Data) is { } action)
try
{
try
if (JsonSerializer.Deserialize<OneBotAction>(e.Data) is { } action)
{
if (_operations.TryGetValue(action.Action, out var type))
try
{
var handler = (IOperation)_service.GetRequiredService(type);
var result = await handler.HandleOperation(_bot, action.Params);
result.Echo = action.Echo;
if (_operations.TryGetValue(action.Action, out var type))
{
var handler = (IOperation)_service.GetRequiredService(type);
var result = await handler.HandleOperation(_bot, action.Params);
result.Echo = action.Echo;

return result;
}
return result;
}

return new OneBotResult(null, 404, "failed") { Echo = action.Echo };
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unexpected error encountered while handling message.");
return new OneBotResult(null, 200, "failed") { Echo = action.Echo };
return new OneBotResult(null, 404, "failed") { Echo = action.Echo };
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Unexpected error encountered while handling message.");
return new OneBotResult(null, 200, "failed") { Echo = action.Echo };
}
}
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Json Serialization failed for such action");
}

_logger.LogWarning("Json Serialization failed for such action");
return null;
}
}