1 using Microsoft.AspNetCore.Builder;
2 using Microsoft.AspNetCore.Http;
3 using Microsoft.AspNetCore.Mvc;
4 using Microsoft.EntityFrameworkCore;
5 using Microsoft.Extensions.DependencyInjection;
6 using Microsoft.Extensions.Logging;
24 static ILogger GetLogger(HttpContext httpContext) => httpContext.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(
ApplicationBuilderExtensions));
32 if (applicationBuilder == null)
33 throw new ArgumentNullException(nameof(applicationBuilder));
34 applicationBuilder.Use(async (context, next) =>
36 var logger = GetLogger(context);
39 await next().ConfigureAwait(
false);
41 catch (DbUpdateException e)
43 if (e.InnerException is OperationCanceledException)
45 logger.LogTrace(
"Rethrowing DbUpdateException as OperationCanceledException: {0}", e);
46 throw e.InnerException;
49 logger.LogDebug(
"Database conflict: {0}", e.Message);
52 AdditionalData = String.Format(CultureInfo.InvariantCulture, (e.InnerException ?? e).Message)
53 }).ExecuteResultAsync(
new ActionContext
56 }).ConfigureAwait(
false);
67 if (applicationBuilder == null)
68 throw new ArgumentNullException(nameof(applicationBuilder));
69 applicationBuilder.Use(async (context, next) =>
71 var logger = GetLogger(context);
74 await next().ConfigureAwait(
false);
76 catch (OperationCanceledException)
78 logger.LogDebug(
"Request cancelled!");
89 if (applicationBuilder == null)
90 throw new ArgumentNullException(nameof(applicationBuilder));
91 applicationBuilder.Use(async (context, next) =>
93 var logger = GetLogger(context);
96 await next().ConfigureAwait(
false);
100 logger.LogError(
"Failed request: {0}", e);
101 await
new ObjectResult(
104 AdditionalData = e.ToString()
107 StatusCode = (int)HttpStatusCode.InternalServerError
109 .ExecuteResultAsync(
new ActionContext
111 HttpContext = context
112 }).ConfigureAwait(
false);
static void UseDbConflictHandling(this IApplicationBuilder applicationBuilder)
Return a ConflictObjectResult for DbUpdateExceptions
ErrorCode
Types of ErrorMessages that the API may return.
Extensions for IApplicationBuilder
static void UseServerErrorHandling(this IApplicationBuilder applicationBuilder)
Suppress all in flight exceptions with error 500.
static void UseCancelledRequestSuppression(this IApplicationBuilder applicationBuilder)
Suppress TaskCanceledException warnings when a user aborts a request
Represents an error message returned by the server