tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ErrorMessageFilter.cs
Go to the documentation of this file.
1using System;
2
3using HotChocolate;
4
5using Microsoft.EntityFrameworkCore;
6using Microsoft.Extensions.Logging;
7
10
12{
17 {
21 readonly ILogger<ErrorMessageFilter> logger;
22
27 public ErrorMessageFilter(ILogger<ErrorMessageFilter> logger)
28 {
29 this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
30 }
31
33 public IError OnError(IError error)
34 {
35 ArgumentNullException.ThrowIfNull(error);
36
37 if (error.Exception == null)
38 return error;
39
40 var errorBuilder = ErrorBuilder.FromError(error)
41 .RemoveException()
42 .ClearExtensions();
43
44 const string ErrorCodeFieldName = "errorCode";
45 const string AdditionalDataFieldName = "additionalData";
46
47 if (error.Exception is DbUpdateException dbUpdateException)
48 {
49 if (dbUpdateException.InnerException is OperationCanceledException)
50 {
51 logger.LogTrace(dbUpdateException, "Rethrowing DbUpdateException as OperationCanceledException");
52 throw dbUpdateException.InnerException;
53 }
54
55 logger.LogDebug(dbUpdateException, "Database conflict!");
56 return errorBuilder
57 .SetMessage(dbUpdateException.Message)
58 .SetExtension(ErrorCodeFieldName, ErrorCode.DatabaseIntegrityConflict)
59 .SetExtension(AdditionalDataFieldName, (dbUpdateException.InnerException ?? dbUpdateException).Message)
60 .Build();
61 }
62
63 if (error.Exception is not ErrorMessageException errorMessageException)
64 {
65 return errorBuilder
66 .SetMessage(error.Exception.Message)
67 .SetExtension(ErrorCodeFieldName, ErrorCode.InternalServerError)
68 .SetExtension(AdditionalDataFieldName, error.Exception.ToString())
69 .Build();
70 }
71
72 return errorBuilder
73 .SetMessage(errorMessageException.Message)
74 .SetExtension(ErrorCodeFieldName, errorMessageException.ErrorCode)
75 .SetExtension(AdditionalDataFieldName, errorMessageException.AdditionalData)
76 .Build();
77 }
78 }
79}
Exception representing ErrorMessageResponses.
IErrorFilter for transforming ErrorMessageResponse-like Exception.
readonly ILogger< ErrorMessageFilter > logger
The ILogger for the ErrorMessageFilter.
ErrorMessageFilter(ILogger< ErrorMessageFilter > logger)
Initializes a new instance of the ErrorMessageFilter class.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12