1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-04-19 16:33:36 +03:00
Octobot/src/Extensions/LoggerExtensions.cs
Octol1ttle c2f7aadaea
Do not use ResultError#IsUserOrEnvironmentError (#289)
In LoggerExtensions#LogResult we skip logging the result if the error is
"user or environment error". What matches that criteria is defined by
Remora's implementation.

However, none of errors defined by the implementation should *ever* happen or be ignored:
* CommandNotFoundError: The client shouldn't send us non-existing
commands. This *can* happen because the client's command list can get
out of sync with the server's, but this happens rarely.
* AmbiguousCommandInvocationError: We don't have commands that would
trigger this error
* RequiredParameterValueMissingError: The client shouldn't send us
commands without required paremeters
* ParameterParsingError: See #220
* ConditionNotSatisfiedError: The client shouldn't send us commands that
don't satisfy our conditions

Closes #220
2024-03-24 15:38:51 +00:00

40 lines
1.4 KiB
C#

using Microsoft.Extensions.Logging;
using Remora.Results;
namespace Octobot.Extensions;
public static class LoggerExtensions
{
/// <summary>
/// Checks if the <paramref name="result" /> has failed due to an error that has resulted from neither invalid user
/// input nor the execution environment and logs the error using the provided <paramref name="logger" />.
/// </summary>
/// <remarks>
/// This has special behavior for <see cref="ExceptionError" /> - its exception will be passed to the
/// <paramref name="logger" />
/// </remarks>
/// <param name="logger">The logger to use.</param>
/// <param name="result">The Result whose error check.</param>
/// <param name="message">The message to use if this result has failed.</param>
public static void LogResult(this ILogger logger, IResult result, string? message = "")
{
if (result.IsSuccess)
{
return;
}
if (result.Error is ExceptionError exe)
{
if (exe.Exception is TaskCanceledException)
{
return;
}
logger.LogError(exe.Exception, "{ErrorMessage}", message);
return;
}
logger.LogWarning("{UserMessage}{NewLine}{ResultErrorMessage}", message, Environment.NewLine,
result.Error.Message);
}
}