Merge pull request #580 from Cyberboss/SuppressRequestCancellation

Supress errors from cancelled requests. Log them as debug
This commit is contained in:
Jordan Brown
2018-08-27 12:06:48 -04:00
committed by GitHub
3 changed files with 28 additions and 1 deletions
@@ -203,7 +203,14 @@ namespace Tgstation.Server.Host.Controllers
Logger.LogDebug("Request made by User ID {0}. Api version: {1}. User-Agent: {2}. Type: {3}. Route {4}{5} to Instance {6}", AuthenticationContext?.User.Id.ToString(CultureInfo.InvariantCulture), ApiHeaders.ApiVersion, ApiHeaders.UserAgent, Request.Method, Request.Path, Request.QueryString, ApiHeaders.InstanceId);
await base.OnActionExecutionAsync(context, next).ConfigureAwait(false);
try
{
await base.OnActionExecutionAsync(context, next).ConfigureAwait(false);
}
catch (OperationCanceledException e)
{
Logger.LogDebug("Request cancelled! Exception: {0}", e);
}
}
}
}
@@ -244,6 +244,8 @@ namespace Tgstation.Server.Host.Core
applicationBuilder.UseDbConflictHandling();
applicationBuilder.UseCancelledRequestSuppression();
applicationBuilder.UseMvc();
}
@@ -35,5 +35,23 @@ namespace Tgstation.Server.Host.Core
}
});
}
/// <summary>
/// Suppress TaskCanceledException warnings when a user aborts a request
/// </summary>
/// <param name="applicationBuilder">The <see cref="IApplicationBuilder"/> to configure</param>
public static void UseCancelledRequestSuppression(this IApplicationBuilder applicationBuilder)
{
if (applicationBuilder == null)
throw new ArgumentNullException(nameof(applicationBuilder));
applicationBuilder.Use(async (context, next) =>
{
try
{
await next().ConfigureAwait(false);
}
catch (OperationCanceledException) { }
});
}
}
}