From ef4d97dfb49a52722d6a338be53a809641b6d2f0 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 3 Feb 2018 14:23:11 -0500 Subject: [PATCH] try catch precondition execution --- src/Discord.Net.Commands/CommandService.cs | 14 ++++++++++++-- .../Results/PreconditionResult.cs | 10 ++++++++-- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index 8e7dab898..8e3dce203 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -1,4 +1,4 @@ -using Discord.Commands.Builders; +using Discord.Commands.Builders; using Discord.Logging; using System; using System.Collections.Concurrent; @@ -287,7 +287,17 @@ namespace Discord.Commands foreach (var match in commands) { - preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, services).ConfigureAwait(false); + try + { + preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, services).ConfigureAwait(false); + } + catch (Exception ex) + { + if (_throwOnError) + throw; + + return PreconditionResult.FromError(ex); + } } var successfulPreconditions = preconditionResults diff --git a/src/Discord.Net.Commands/Results/PreconditionResult.cs b/src/Discord.Net.Commands/Results/PreconditionResult.cs index ca65a373e..b8c96c754 100644 --- a/src/Discord.Net.Commands/Results/PreconditionResult.cs +++ b/src/Discord.Net.Commands/Results/PreconditionResult.cs @@ -1,17 +1,21 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; namespace Discord.Commands { [DebuggerDisplay(@"{DebuggerDisplay,nq}")] public class PreconditionResult : IResult { + public Exception Exception { get; } + public CommandError? Error { get; } public string ErrorReason { get; } public bool IsSuccess => !Error.HasValue; - protected PreconditionResult(CommandError? error, string errorReason) + protected PreconditionResult(CommandError? error, string errorReason, Exception exception = null) { + Exception = exception; Error = error; ErrorReason = errorReason; } @@ -20,6 +24,8 @@ namespace Discord.Commands => new PreconditionResult(null, null); public static PreconditionResult FromError(string reason) => new PreconditionResult(CommandError.UnmetPrecondition, reason); + public static PreconditionResult FromError(Exception ex) + => new PreconditionResult(CommandError.Exception, ex.Message, ex); public static PreconditionResult FromError(IResult result) => new PreconditionResult(result.Error, result.ErrorReason);