| | | 1 | | namespace ZhEaIsNsAaBn.Infrastructure.Core; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// The shared error ladder (STYLE_GUIDE §6): runs a database call and turns its exception into an <see cref="Error"/>. |
| | | 5 | | /// Cancellation always propagates; a concurrency conflict is classified here (provider-neutral); everything else goes t |
| | | 6 | | /// context's <see cref="IDbErrorClassifier"/> (from its provider package), else <see cref="Error.Unknown"/>. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class SafeExecution |
| | | 9 | | { |
| | | 10 | | /// <summary>Runs <paramref name="action"/> and returns its result, or the classified error.</summary> |
| | | 11 | | /// <typeparam name="T">The result type.</typeparam> |
| | | 12 | | /// <param name="action">The database call, e.g. <c>() => dbContext.SaveChanges()</c>.</param> |
| | | 13 | | /// <param name="classifier">The provider classifier (<c>dbContext.GetDbErrorClassifier()</c>); <see langword="null" |
| | | 14 | | /// <returns> |
| | | 15 | | /// The result on success; otherwise <c>ConcurrencyException</c>, the classifier's error, or <see cref="Error.Unknow |
| | | 16 | | /// </returns> |
| | | 17 | | /// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception> |
| | | 18 | | /// <exception cref="OperationCanceledException">The call was cancelled.</exception> |
| | | 19 | | public static LayerResponse<T> ExecuteSafe< T >( Func<T> action, IDbErrorClassifier? classifier = null ) |
| | | 20 | | { |
| | 340 | 21 | | ArgumentNullException.ThrowIfNull( action ); |
| | | 22 | | |
| | 340 | 23 | | try { return action(); } |
| | 0 | 24 | | catch ( OperationCanceledException ) { throw; } |
| | 4 | 25 | | catch ( DbUpdateConcurrencyException ex ) { return ex.Classify(); } |
| | 72 | 26 | | catch ( Exception ex ) { return classifier?.Classify( ex ) ?? Error.Unknown( ex ); } |
| | 340 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary>Async version of <see cref="ExecuteSafe{T}(Func{T}, IDbErrorClassifier)"/>.</summary> |
| | | 30 | | /// <typeparam name="T">The result type.</typeparam> |
| | | 31 | | /// <param name="action">The database call, e.g. <c>() => dbContext.SaveChangesAsync( cancellationToken )</c>.</p |
| | | 32 | | /// <param name="classifier">The provider classifier (<c>dbContext.GetDbErrorClassifier()</c>); <see langword="null" |
| | | 33 | | /// <returns> |
| | | 34 | | /// The result on success; otherwise <c>ConcurrencyException</c>, the classifier's error, or <see cref="Error.Unknow |
| | | 35 | | /// </returns> |
| | | 36 | | /// <exception cref="ArgumentNullException"><paramref name="action"/> is <see langword="null"/>.</exception> |
| | | 37 | | /// <exception cref="OperationCanceledException">The call was cancelled.</exception> |
| | | 38 | | public static async Task<LayerResponse<T>> ExecuteSafeAsync< T >( Func<Task<T>> action, IDbErrorClassifier? classifi |
| | | 39 | | { |
| | 252 | 40 | | ArgumentNullException.ThrowIfNull( action ); |
| | | 41 | | |
| | 252 | 42 | | try { return await action().ConfigureAwait( false ); } |
| | 8 | 43 | | catch ( OperationCanceledException ) { throw; } |
| | 0 | 44 | | catch ( DbUpdateConcurrencyException ex ) { return ex.Classify(); } |
| | 136 | 45 | | catch ( Exception ex ) { return classifier?.Classify( ex ) ?? Error.Unknown( ex ); } |
| | 248 | 46 | | } |
| | | 47 | | } |