1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-05-16 10:46:08 +03:00

Log result failures with stack traces (#282)

This feature will improve the debugging experience for developers by
providing the information about *where exactly* a result has failed

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2024-03-20 23:08:16 +05:00 committed by GitHub
parent 5cc04e9cc3
commit 309d900067
Signed by: GitHub
GPG key ID: B5690EEEBB952194
21 changed files with 145 additions and 71 deletions

View file

@ -20,7 +20,7 @@ public static class ChannelApiExtensions
{
if (!embedResult.IsDefined() || !embedResult.Value.IsDefined(out var embed))
{
return Result.FromError(embedResult.Value);
return ResultExtensions.FromError(embedResult.Value);
}
return (Result)await channelApi.CreateMessageAsync(channelId, message, nonce, isTextToSpeech, new[] { embed },

View file

@ -13,7 +13,7 @@ public static class FeedbackServiceExtensions
{
if (!embedResult.IsDefined(out var embed))
{
return Result.FromError(embedResult);
return ResultExtensions.FromError(embedResult);
}
return (Result)await feedback.SendContextualEmbedAsync(embed, options, ct);

View file

@ -0,0 +1,61 @@
using System.Diagnostics;
using Microsoft.Extensions.Logging;
using Remora.Results;
namespace Octobot.Extensions;
public static class ResultExtensions
{
public static Result FromError(Result result)
{
LogResultStackTrace(result);
return result;
}
public static Result FromError<T>(Result<T> result)
{
var casted = (Result)result;
LogResultStackTrace(casted);
return casted;
}
[Conditional("DEBUG")]
private static void LogResultStackTrace(Result result)
{
if (Octobot.StaticLogger is null || result.IsSuccess)
{
return;
}
Octobot.StaticLogger.LogError("{ErrorType}: {ErrorMessage}{NewLine}{StackTrace}",
result.Error.GetType().FullName, result.Error.Message, Environment.NewLine, ConstructStackTrace());
var inner = result.Inner;
while (inner is { IsSuccess: false })
{
Octobot.StaticLogger.LogError("Caused by: {ResultType}: {ResultMessage}",
inner.Error.GetType().FullName, inner.Error.Message);
inner = inner.Inner;
}
}
private static string ConstructStackTrace()
{
var stackArray = new StackTrace(3, true).ToString().Split(Environment.NewLine).ToList();
for (var i = stackArray.Count - 1; i >= 0; i--)
{
var frame = stackArray[i];
var trimmed = frame.TrimStart();
if (trimmed.StartsWith("at System.Threading", StringComparison.Ordinal)
|| trimmed.StartsWith("at System.Runtime.CompilerServices", StringComparison.Ordinal))
{
stackArray.RemoveAt(i);
}
}
return string.Join(Environment.NewLine, stackArray);
}
}