From 3fb21e06c23dc1b86e3a6274f72358b5a59acb2b Mon Sep 17 00:00:00 2001 From: RogueException Date: Sat, 18 Mar 2017 21:38:28 -0300 Subject: [PATCH] Fixed RunMode.Sync running Async. Added ThrowOnError option. --- src/Discord.Net.Commands/CommandService.cs | 3 ++- src/Discord.Net.Commands/CommandServiceConfig.cs | 6 ++++++ src/Discord.Net.Commands/Info/CommandInfo.cs | 5 +++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Commands/CommandService.cs b/src/Discord.Net.Commands/CommandService.cs index e8db0100d..91472b9eb 100644 --- a/src/Discord.Net.Commands/CommandService.cs +++ b/src/Discord.Net.Commands/CommandService.cs @@ -24,7 +24,7 @@ namespace Discord.Commands private readonly HashSet _moduleDefs; private readonly CommandMap _map; - internal readonly bool _caseSensitive; + internal readonly bool _caseSensitive, _throwOnError; internal readonly char _separatorChar; internal readonly RunMode _defaultRunMode; internal readonly Logger _cmdLogger; @@ -38,6 +38,7 @@ namespace Discord.Commands public CommandService(CommandServiceConfig config) { _caseSensitive = config.CaseSensitiveCommands; + _throwOnError = config.ThrowOnError; _separatorChar = config.SeparatorChar; _defaultRunMode = config.DefaultRunMode; if (_defaultRunMode == RunMode.Default) diff --git a/src/Discord.Net.Commands/CommandServiceConfig.cs b/src/Discord.Net.Commands/CommandServiceConfig.cs index a94e433f5..b0925e28d 100644 --- a/src/Discord.Net.Commands/CommandServiceConfig.cs +++ b/src/Discord.Net.Commands/CommandServiceConfig.cs @@ -11,5 +11,11 @@ /// Gets or sets the minimum log level severity that will be sent to the Log event. public LogSeverity LogLevel { get; set; } = LogSeverity.Info; + + /// + /// Gets or sets whether RunMode.Sync commands should push exceptions up to the caller. + /// If false or an RunMode.Async command, exceptions are only reported in the Log event. + /// + public bool ThrowOnError { get; set; } = true; } } diff --git a/src/Discord.Net.Commands/Info/CommandInfo.cs b/src/Discord.Net.Commands/Info/CommandInfo.cs index 26b6163ab..d0bf25a4b 100644 --- a/src/Discord.Net.Commands/Info/CommandInfo.cs +++ b/src/Discord.Net.Commands/Info/CommandInfo.cs @@ -140,7 +140,7 @@ namespace Discord.Commands switch (RunMode) { case RunMode.Sync: //Always sync - var t1 = ExecuteAsyncInternal(context, args, map); + await ExecuteAsyncInternal(context, args, map).ConfigureAwait(false); break; case RunMode.Async: //Always async var t2 = Task.Run(async () => @@ -168,7 +168,8 @@ namespace Discord.Commands { ex = new CommandException(this, context, ex); await Module.Service._cmdLogger.ErrorAsync(ex).ConfigureAwait(false); - throw; + if (Module.Service._throwOnError) + throw; } await Module.Service._cmdLogger.VerboseAsync($"Executed {GetLogText(context)}").ConfigureAwait(false); }