tgstation-server  4.3.2
The /tg/station 13 server suite
ApplicationBuilderExtensions.cs
Go to the documentation of this file.
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;
7 using System;
8 using System.Globalization;
9 using System.Net;
11 
12 namespace Tgstation.Server.Host.Extensions
13 {
18  {
24  static ILogger GetLogger(HttpContext httpContext) => httpContext.RequestServices.GetRequiredService<ILoggerFactory>().CreateLogger(typeof(ApplicationBuilderExtensions));
25 
30  public static void UseDbConflictHandling(this IApplicationBuilder applicationBuilder)
31  {
32  if (applicationBuilder == null)
33  throw new ArgumentNullException(nameof(applicationBuilder));
34  applicationBuilder.Use(async (context, next) =>
35  {
36  var logger = GetLogger(context);
37  try
38  {
39  await next().ConfigureAwait(false);
40  }
41  catch (DbUpdateException e)
42  {
43  logger.LogDebug("Database conflict: {0}", e.Message);
44  await new ConflictObjectResult(new ErrorMessage(ErrorCode.DatabaseIntegrityConflict)
45  {
46  AdditionalData = String.Format(CultureInfo.InvariantCulture, (e.InnerException ?? e).Message)
47  }).ExecuteResultAsync(new ActionContext
48  {
49  HttpContext = context
50  }).ConfigureAwait(false);
51  }
52  });
53  }
54 
59  public static void UseCancelledRequestSuppression(this IApplicationBuilder applicationBuilder)
60  {
61  if (applicationBuilder == null)
62  throw new ArgumentNullException(nameof(applicationBuilder));
63  applicationBuilder.Use(async (context, next) =>
64  {
65  var logger = GetLogger(context);
66  try
67  {
68  await next().ConfigureAwait(false);
69  }
70  catch (OperationCanceledException)
71  {
72  logger.LogDebug("Request cancelled!");
73  }
74  });
75  }
76 
81  public static void UseServerErrorHandling(this IApplicationBuilder applicationBuilder)
82  {
83  if (applicationBuilder == null)
84  throw new ArgumentNullException(nameof(applicationBuilder));
85  applicationBuilder.Use(async (context, next) =>
86  {
87  var logger = GetLogger(context);
88  try
89  {
90  await next().ConfigureAwait(false);
91  }
92  catch (Exception e)
93  {
94  logger.LogError("Failed request: {0}", e);
95  await new ObjectResult(
96  new ErrorMessage(ErrorCode.InternalServerError)
97  {
98  AdditionalData = e.ToString()
99  })
100  {
101  StatusCode = (int)HttpStatusCode.InternalServerError
102  }
103  .ExecuteResultAsync(new ActionContext
104  {
105  HttpContext = context
106  }).ConfigureAwait(false);
107  }
108  });
109  }
110  }
111 }
static void UseDbConflictHandling(this IApplicationBuilder applicationBuilder)
Return a ConflictObjectResult for DbUpdateExceptions
ErrorCode
Types of ErrorMessages that the API may return.
Definition: ErrorCode.cs:10
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
Definition: ErrorMessage.cs:9