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 logger.LogDebug(
"Database conflict: {0}", e.Message);
46 AdditionalData = String.Format(CultureInfo.InvariantCulture, (e.InnerException ?? e).Message)
47 }).ExecuteResultAsync(
new ActionContext
50 }).ConfigureAwait(
false);
61 if (applicationBuilder == null)
62 throw new ArgumentNullException(nameof(applicationBuilder));
63 applicationBuilder.Use(async (context, next) =>
65 var logger = GetLogger(context);
68 await next().ConfigureAwait(
false);
70 catch (OperationCanceledException)
72 logger.LogDebug(
"Request cancelled!");
83 if (applicationBuilder == null)
84 throw new ArgumentNullException(nameof(applicationBuilder));
85 applicationBuilder.Use(async (context, next) =>
87 var logger = GetLogger(context);
90 await next().ConfigureAwait(
false);
94 logger.LogError(
"Failed request: {0}", e);
95 await
new ObjectResult(
98 AdditionalData = e.ToString()
101 StatusCode = (int)HttpStatusCode.InternalServerError
103 .ExecuteResultAsync(
new ActionContext
105 HttpContext = context
106 }).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