| | | 1 | | namespace ZhEaIsNsAaBn.Utilities.APIExceptionHandlers.EntityFrameworkCore; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Handles EF Core save failures with the same <see cref="Error"/> a repository returns, so the status is the same too |
| | | 5 | | /// unique / concurrency 409, other <c>DatabaseError</c> 500): |
| | | 6 | | /// <list type="bullet"> |
| | | 7 | | /// <item><see cref="DbUpdateConcurrencyException"/> → Core's <c>Classify()</c> (<c>ConcurrencyException</c>).</item> |
| | | 8 | | /// <item><see cref="DbUpdateException"/> → the failing context's <c>IDbErrorClassifier</c> (from <c>Entries[0].Context< |
| | | 9 | | /// by <c>WithSqlServer()</c>); <see cref="Error.Unknown"/> when there is no entry, no classifier, the classifier doesn' |
| | | 10 | | /// the context is already disposed.</item> |
| | | 11 | | /// </list> |
| | | 12 | | /// Other exceptions — a bare provider exception such as <c>SqlException</c> included — are left to the next handler (th |
| | | 13 | | /// </summary> |
| | | 14 | | public sealed class DatabaseExceptionHandler : BaseExceptionHandler<DatabaseExceptionHandler>, IExceptionHandler |
| | | 15 | | { |
| | | 16 | | /// <summary>Creates the handler.</summary> |
| | | 17 | | /// <param name="logger">The logger.</param> |
| | | 18 | | /// <param name="environment">The host environment.</param> |
| | | 19 | | /// <param name="settings">The status mapping.</param> |
| | | 20 | | public DatabaseExceptionHandler( ILogger<DatabaseExceptionHandler> logger, IWebHostEnvironment environment, IOptions |
| | 60 | 21 | | : base( logger, environment, settings ) { } |
| | | 22 | | |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public async ValueTask<bool> TryHandleAsync( HttpContext httpContext, Exception exception, CancellationToken cancell |
| | | 26 | | { |
| | 30 | 27 | | ArgumentNullException.ThrowIfNull( httpContext ); |
| | | 28 | | |
| | 30 | 29 | | var error = Classify( exception ); |
| | | 30 | | |
| | 39 | 31 | | if ( error is null ) { return false; } |
| | | 32 | | |
| | | 33 | | |
| | 21 | 34 | | Log.DatabaseError( Logger, exception, error.Code ); |
| | | 35 | | |
| | | 36 | | |
| | 21 | 37 | | await GenerateError( httpContext, error, cancellationToken ).ConfigureAwait( false ); |
| | | 38 | | |
| | 21 | 39 | | return true; |
| | 30 | 40 | | } |
| | | 41 | | |
| | | 42 | | |
| | | 43 | | // The error for an EF Core save failure, or null when the exception isn't one. |
| | | 44 | | private static Error? Classify( Exception exception ) |
| | 30 | 45 | | => exception switch |
| | 30 | 46 | | { |
| | 6 | 47 | | DbUpdateConcurrencyException concurrency => concurrency.Classify(), |
| | 15 | 48 | | DbUpdateException update => ClassifyWithContext( update ) ?? Error.Unknown( update ), |
| | 9 | 49 | | _ => null |
| | 30 | 50 | | }; |
| | | 51 | | |
| | | 52 | | private static Error? ClassifyWithContext( DbUpdateException exception ) |
| | | 53 | | { |
| | 18 | 54 | | if ( exception.Entries.Count == 0 ) { return null; } |
| | | 55 | | |
| | | 56 | | |
| | 12 | 57 | | try { return exception.Entries[ 0 ].Context.GetDbErrorClassifier()?.Classify( exception ); } |
| | 3 | 58 | | catch ( ObjectDisposedException ) |
| | | 59 | | { |
| | | 60 | | // e.g. `using var context = factory.CreateDbContext()` already left: its services are gone. |
| | 3 | 61 | | return null; |
| | | 62 | | } |
| | 12 | 63 | | } |
| | | 64 | | } |