1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:07:25 +08:00

Catch multiplayer client-related unobserved exceptions better

Silencing an exception from a task continuation requires accessing
`task.Exception` in any way, which was not done previously if
`logOnError` was false.

To resolve without having to worry whether the compiler will optimise
away a useless access or now, just always log, but switch the logging
level. The unimportant errors will be logged as debug and therefore
essentially silenced on release builds (but could still be potentially
useful in debugging).
This commit is contained in:
Bartłomiej Dach 2020-12-31 11:29:02 +01:00
parent ec75efe124
commit 2d279350ad

View File

@ -1,7 +1,11 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
#nullable enable
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework.Extensions.ExceptionExtensions;
using osu.Framework.Logging; using osu.Framework.Logging;
namespace osu.Game.Extensions namespace osu.Game.Extensions
@ -13,13 +17,19 @@ namespace osu.Game.Extensions
/// Avoids unobserved exceptions from being fired. /// Avoids unobserved exceptions from being fired.
/// </summary> /// </summary>
/// <param name="task">The task.</param> /// <param name="task">The task.</param>
/// <param name="logOnError">Whether errors should be logged as important, or silently ignored.</param> /// <param name="logAsError">
public static void CatchUnobservedExceptions(this Task task, bool logOnError = false) /// Whether errors should be logged as errors visible to users, or as debug messages.
/// Logging as debug will essentially silence the errors on non-release builds.
/// </param>
public static void CatchUnobservedExceptions(this Task task, bool logAsError = false)
{ {
task.ContinueWith(t => task.ContinueWith(t =>
{ {
if (logOnError) Exception? exception = t.Exception?.AsSingular();
Logger.Log($"Error running task: {t.Exception?.Message ?? "unknown"}", LoggingTarget.Runtime, LogLevel.Important); if (logAsError)
Logger.Error(exception, $"Error running task: {exception?.Message ?? "(unknown)"}", LoggingTarget.Runtime, true);
else
Logger.Log($"Error running task: {exception}", LoggingTarget.Runtime, LogLevel.Debug);
}, TaskContinuationOptions.NotOnRanToCompletion); }, TaskContinuationOptions.NotOnRanToCompletion);
} }
} }