| | | 1 | | // ReSharper disable once CheckNamespace |
| | | 2 | | namespace Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Turns SQL Server exceptions into an <see cref="Error"/> by error number. Additional data holds SQL details for logs |
| | | 6 | | /// (<see cref="Error.AdditionalData"/> is never serialized). No connection string, password or token is ever included. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class SqlServerDbExceptionExtensions |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Classifies a failed save by the inner <see cref="SqlException"/> number. Additional data: <c>Number</c>, <c>Line |
| | | 12 | | /// <c>Message</c> (server message), <c>Procedure</c>; plus <c>Database</c> and <c>User</c> when <see cref="Exceptio |
| | | 13 | | /// <c>ConnectionString</c>. The connection string, password and tokens are never copied. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="exception">The exception.</param> |
| | | 16 | | /// <returns> |
| | | 17 | | /// One of <c>UniqueConstraintViolation</c>, <c>ConstraintViolation</c>, <c>RequiredFieldMissing</c>, <c>DataTooLong |
| | | 18 | | /// <c>NumericOverflow</c>, <c>InvalidDataType</c>, <c>Deadlock</c>, <c>Timeout</c>, <c>UnexpectedError</c>. |
| | | 19 | | /// </returns> |
| | | 20 | | /// <exception cref="ArgumentNullException"><paramref name="exception"/> is <see langword="null"/>.</exception> |
| | | 21 | | public static Error Classify( this DbUpdateException exception ) |
| | | 22 | | { |
| | 39 | 23 | | ArgumentNullException.ThrowIfNull( exception ); |
| | | 24 | | |
| | 39 | 25 | | if ( exception.InnerException is not SqlException sqlException ) { return DbUpdateExceptions.UnexpectedError( ex |
| | | 26 | | |
| | | 27 | | |
| | 39 | 28 | | var additionalData = new Dictionary<string, object> |
| | 39 | 29 | | { |
| | 39 | 30 | | [ "Number" ] = sqlException.Number, |
| | 39 | 31 | | [ "Message" ] = sqlException.Message, |
| | 39 | 32 | | [ "Procedure" ] = sqlException.Procedure, |
| | 39 | 33 | | [ "LineNumber" ] = sqlException.LineNumber |
| | 39 | 34 | | }; |
| | | 35 | | |
| | 39 | 36 | | AddDatabaseAndUser( additionalData, exception.Data[ "ConnectionString" ] as string ); |
| | | 37 | | |
| | | 38 | | |
| | 39 | 39 | | return sqlException.Number switch |
| | 39 | 40 | | { |
| | 7 | 41 | | 2627 or 2601 => DbUpdateExceptions.UniqueConstraintViolation( exception, additionalData ), |
| | 0 | 42 | | 547 => DbUpdateExceptions.ConstraintViolation( exception, additionalData ), |
| | 0 | 43 | | 515 => DbUpdateExceptions.RequiredFieldMissing( exception, additionalData ), |
| | 0 | 44 | | 8152 => DbUpdateExceptions.DataTooLong( exception, additionalData ), |
| | 0 | 45 | | 8115 => DbUpdateExceptions.NumericOverflow( exception, additionalData ), |
| | 0 | 46 | | 245 or 241 or 242 => DbUpdateExceptions.InvalidDataType( exception, additionalData ), |
| | 0 | 47 | | 1205 => DbUpdateExceptions.Deadlock( exception, additionalData ), |
| | 0 | 48 | | -2 => DbUpdateExceptions.Timeout( exception, additionalData ), |
| | 32 | 49 | | _ => DbUpdateExceptions.UnexpectedError( exception, additionalData ) |
| | 39 | 50 | | }; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary>Classifies a <see cref="SqlException"/> by its number. Additional data: <c>Number</c>, <c>LineNumber</c |
| | | 54 | | /// <param name="exception">The exception.</param> |
| | | 55 | | /// <returns>An error from <see cref="SqlExceptions"/>; <c>Unknown</c> when the number isn't mapped.</returns> |
| | | 56 | | /// <exception cref="ArgumentNullException"><paramref name="exception"/> is <see langword="null"/>.</exception> |
| | | 57 | | public static Error Classify( this SqlException exception ) |
| | | 58 | | { |
| | 7 | 59 | | ArgumentNullException.ThrowIfNull( exception ); |
| | | 60 | | |
| | 7 | 61 | | var additionalData = new Dictionary<string, object> |
| | 7 | 62 | | { |
| | 7 | 63 | | [ "Number" ] = exception.Number, |
| | 7 | 64 | | [ "LineNumber" ] = exception.LineNumber, |
| | 7 | 65 | | [ "Procedure" ] = exception.Procedure |
| | 7 | 66 | | }; |
| | | 67 | | |
| | | 68 | | |
| | 7 | 69 | | return exception.Number switch |
| | 7 | 70 | | { |
| | 0 | 71 | | -1 or 2 or 1225 or 4060 => SqlExceptions.DatabaseNotAccessible( exception, additionalData ), |
| | 0 | 72 | | -2 or 8645 or 30053 or 1222 => SqlExceptions.RequestTimeout( exception, additionalData ), |
| | 0 | 73 | | 18456 => SqlExceptions.DataBaseAuthenticationFailed( exception, additional |
| | 0 | 74 | | 241 => SqlExceptions.InvalidData( exception, additionalData ), |
| | 0 | 75 | | 512 or 511 => SqlExceptions.TooManyRows( exception, additionalData ), |
| | 4 | 76 | | 515 => SqlExceptions.Null( exception, additionalData ), |
| | 0 | 77 | | 1205 => SqlExceptions.DeadlockVictim( exception, additionalData ), |
| | 0 | 78 | | 2627 or 2601 => SqlExceptions.UniqueConstraint( exception, additionalData ), |
| | 0 | 79 | | 547 => SqlExceptions.ForeignKey( exception, additionalData ), |
| | 0 | 80 | | 8152 => SqlExceptions.StringTooLong( exception, additionalData ), |
| | 0 | 81 | | 8115 => SqlExceptions.InvalidType( exception, additionalData ), |
| | 3 | 82 | | 50000 => SqlExceptions.CustomException( exception, additionalData ), |
| | 0 | 83 | | 701 or 8651 => SqlExceptions.OutOfMemory( exception, additionalData ), |
| | 0 | 84 | | 1204 => SqlExceptions.OutOfLocks( exception, additionalData ), |
| | 0 | 85 | | _ => SqlExceptions.Unknown( exception, additionalData ) |
| | 7 | 86 | | }; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Adds <c>Database</c> (initial catalog) and <c>User</c> (user ID, else the authentication mode) from a connection |
| | | 92 | | /// The string itself, the password and any token are never stored; an unreadable string adds nothing. |
| | | 93 | | /// </summary> |
| | | 94 | | private static void AddDatabaseAndUser( Dictionary<string, object> additionalData, string? connectionString ) |
| | | 95 | | { |
| | 50 | 96 | | if ( string.IsNullOrWhiteSpace( connectionString ) ) { return; } |
| | | 97 | | |
| | | 98 | | |
| | | 99 | | SqlConnectionStringBuilder builder; |
| | | 100 | | |
| | 56 | 101 | | try { builder = new SqlConnectionStringBuilder( connectionString ); } |
| | 0 | 102 | | catch ( ArgumentException ) { return; } // malformed: nothing safe to extract |
| | 0 | 103 | | catch ( FormatException ) { return; } |
| | | 104 | | |
| | | 105 | | |
| | 56 | 106 | | if ( !string.IsNullOrWhiteSpace( builder.InitialCatalog ) ) { additionalData[ "Database" ] = builder.InitialCata |
| | | 107 | | |
| | | 108 | | |
| | 28 | 109 | | var user = !string.IsNullOrWhiteSpace( builder.UserID ) ? builder.UserID |
| | 28 | 110 | | : builder.Authentication != SqlAuthenticationMethod.NotSpecified ? builder.Authentication.ToString() |
| | 28 | 111 | | : builder.IntegratedSecurity ? "Integrated Security" |
| | 28 | 112 | | : null; |
| | | 113 | | |
| | 56 | 114 | | if ( user is not null ) { additionalData[ "User" ] = user; } |
| | 28 | 115 | | } |
| | | 116 | | } |