public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var dbEx = ADOExceptionHelper.ExtractDbException(exInfo.SqlException);

            if (dbEx is SqlException sqle)
            {
                if (sqle.Number == 547)
                {
                    return(new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null));
                }
                if (sqle.Number == 208)
                {
                    return(new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql));
                }
            }

            if (dbEx is Microsoft.Data.SqlClient.SqlException msSqle)
            {
                if (msSqle.Number == 547)
                {
                    return(new ConstraintViolationException(exInfo.Message, msSqle.InnerException, exInfo.Sql, null));
                }
                if (msSqle.Number == 208)
                {
                    return(new SQLGrammarException(exInfo.Message, msSqle.InnerException, exInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Beispiel #2
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 2601:     // Violation in unique index
                case 2627:     // Violation in unique constraint
                    return(new ConstraintViolationException(exInfo.Message, sqle, exInfo.Sql, null));

                case 547:     // constraint violation (referential integrity)
                {
                    if (exInfo.Message.StartsWith("could not delete"))
                    {
                        return(new CannotDeleteException(exInfo.Message, sqle));
                    }

                    return(new ConstraintViolationException(exInfo.Message, sqle, exInfo.Sql, null));
                }
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                                 exInfo.Message, exInfo.Sql));
        }
Beispiel #3
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var dbException = ADOExceptionHelper.ExtractDbException(exInfo.SqlException);

            var ns = dbException.GetType().Namespace ?? string.Empty;

            if (ns.ToLowerInvariant().StartsWith("system.data.sqlite"))
            {
                // SQLite exception
                switch (dbException.ErrorCode)
                {
                case -2147467259: // Abort due to constraint violation
                    throw new ConcurrencyException();
                }
            }

            if (ns.ToLowerInvariant().StartsWith("system.data.sqlclient"))
            {
                // MS SQL Server
                switch (dbException.ErrorCode)
                {
                case -2146232060:
                    throw new ConcurrencyException();
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                                 exInfo.Message, exInfo.Sql));
        }
Beispiel #4
0
 /// <summary>
 /// Creates a SqlExceptionMapping that will match if the error code in
 /// the SqlException matches any of the error codes passed to this function.
 /// </summary>
 /// <param name="errorCodes">The error codes on the SqlException that
 /// will cause the mapping to match.</param>
 /// <param name="converter">The function that will be used to convert the exception.</param>
 public static SqlExceptionMapping MatchOnSqlErrorCode(IEnumerable <int> errorCodes, Func <AdoExceptionContextInfo, Exception> converter)
 {
     return(new SqlExceptionMapping {
         Matcher = c => {
             var sqlException = ADOExceptionHelper.ExtractDbException(c.SqlException) as SqlException;
             return sqlException != null && errorCodes.Contains(sqlException.ErrorCode);
         },
         Converter = converter
     });
 }
Beispiel #5
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if ((sqle != null) && (sqle.Number == 3960))
            {
                return(new StaleObjectStateException(exInfo.EntityName, exInfo.EntityId));
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
		public Exception Convert(AdoExceptionContextInfo exInfo)
		{
			SqlException sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;
			if(sqle != null)
			{
				if (sqle.Number == 547)
					return new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null);
				if (sqle.Number == 208)
					return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
			}
			return SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql);
		}
Beispiel #7
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqlException = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as MySqlException;

            if (sqlException != null)
            {
                switch ((MySqlErrorCode)sqlException.Number)
                {
                case MySqlErrorCode.DuplicateKeyEntry:
                    return(new ConstraintViolationException(exInfo.Message, sqlException.InnerException, exInfo.Sql, null));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Beispiel #8
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqlException = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SQLiteException;

            if (sqlException != null)
            {
                switch (sqlException.ErrorCode)
                {
                case 19:
                    return(new ConstraintViolationException(exInfo.Message, sqlException.InnerException, exInfo.Sql, null));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
        public Exception Convert(AdoExceptionContextInfo adoExceptionContextInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(adoExceptionContextInfo.SqlException) as DbException;

            if (sqle != null)
            {
                if (sqle.ErrorCode == 335544466)
                {
                    return(new ConstraintViolationException(adoExceptionContextInfo.Message, sqle.InnerException, adoExceptionContextInfo.Sql, null));
                }
                if (sqle.ErrorCode == 335544569)
                {
                    return(new SQLGrammarException(adoExceptionContextInfo.Message, sqle.InnerException, adoExceptionContextInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(adoExceptionContextInfo.SqlException, adoExceptionContextInfo.Message, adoExceptionContextInfo.Sql));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as OracleException;

            if (sqle != null)
            {
                if (sqle.Code == 1036)
                {
                    return(new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null));
                }
                if (sqle.Code == 942)
                {
                    return(new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as DbException;

            if (sqle != null)
            {
                string code = (string)sqle.GetType().GetProperty("Code").GetValue(sqle, null);

                if (code == "23503")
                {
                    return(new ConstraintViolationException(exInfo.Message, sqle.InnerException, exInfo.Sql, null));
                }
                if (code == "42P01")
                {
                    return(new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Beispiel #12
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 547:
                    return(new ConstraintViolationException(exInfo.Message, sqle));

                case 2627:
                    return(new UniquenessViolationException(exInfo.Message, sqle));
                    //case 208:
                    //    return new SQLGrammarException(exInfo.Message, sqle.InnerException, exInfo.Sql);
                    //case 3960:
                    //    return new StaleObjectStateException(exInfo.EntityName, exInfo.EntityId);
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException, exInfo.Message, exInfo.Sql));
        }
Beispiel #13
0
        public Exception Convert(AdoExceptionContextInfo exceptionContext)
        {
            var sqlException = ADOExceptionHelper.ExtractDbException(exceptionContext.SqlException) as SqlException;

            if (sqlException != null)
            {
                switch (sqlException.Number)
                {
                case 2627:
                case 2601:
                case 547:
                    return(new ConstraintViolationException(
                               exceptionContext.SqlException.Message,
                               sqlException.InnerException,
                               exceptionContext.Sql,
                               null
                               ));

                case 208:
                    return(new SQLGrammarException(
                               exceptionContext.SqlException.Message,
                               sqlException.InnerException,
                               exceptionContext.Sql
                               ));

                case 3960:
                    return(new StaleObjectStateException(
                               exceptionContext.EntityName,
                               exceptionContext.EntityId
                               ));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(
                       exceptionContext.SqlException,
                       exceptionContext.Message,
                       exceptionContext.Sql
                       ));
        }
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 17:
                // SQL Server does not exist or access denied.
                case 4060:
                // Invalid Database
                case 18456:
                    // Login Failed
                    return(new DatabaseException(sqle.Message, sqle));

                case 547:
                    // ForeignKey Violation
                    return(new ConstraintException(_ParseConstraintName(sqle.Message), sqle));

                case 1205:
                    // DeadLock Victim
                    return(new DatabaseException(sqle.Message, sqle));

                case 2627:
                case 2601:
                    // Unique Index/Constriant Violation
                    return(new ConstraintException(_ParseConstraintName(sqle.Message), sqle));

                default:
                    // throw a general DAL Exception
                    return(new DatabaseException(sqle.Message, sqle));
                }
            }

            return(SQLStateConverter.HandledNonSpecificException(
                       exInfo.SqlException,
                       exInfo.Message, exInfo.Sql));
        }
            public Exception Convert(AdoExceptionContextInfo contextInfo)
            {
                Exception result = null;
                var       sqle   = ADOExceptionHelper.ExtractDbException(contextInfo.SqlException) as SqlException;

                if (sqle != null)
                {
                    switch (sqle.Number)
                    {
                    case 547:
                        result = new ConstraintViolationException(
                            sqle.Message,
                            sqle,
                            contextInfo.Sql,
                            null);
                        break;

                    case 208:
                        result = new SQLGrammarException(
                            contextInfo.Message,
                            sqle,
                            contextInfo.Sql);
                        break;

                    case 3960:
                        result = new StaleObjectStateException(
                            contextInfo.EntityName,
                            contextInfo.EntityId);
                        break;
                    }
                }

                return(result ?? SQLStateConverter.HandledNonSpecificException(
                           contextInfo.SqlException,
                           contextInfo.Message,
                           contextInfo.Sql));
            }
        private static Exception TranslateSqlException(Exception e)
        {
            // code here is based on ideas found in the following blog post:
            // http://fabiomaulo.blogspot.com/2009/06/improving-ado-exception-management-in.html

            var sqle = ADOExceptionHelper.ExtractDbException(e) as SqlException;

            // sqle is non-null only for MSSQL (SqlClient driver)
            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case 547:
                    return(new ConstraintViolationException(e.Message, sqle.InnerException, null));

                case 208:
                    return(new SQLGrammarException(e.Message, sqle.InnerException));

                case 1205:
                    return(new LockAcquisitionException(e.Message, sqle.InnerException));
                }
            }
            return(e);
        }
Beispiel #17
0
        public Exception Convert(AdoExceptionContextInfo exInfo)
        {
            var sqle = ADOExceptionHelper.ExtractDbException(exInfo.SqlException) as SqlException;

            if (sqle != null)
            {
                switch (sqle.Number)
                {
                case -2:          // timeout
                case -2147217871: // timeout
                case 11:          // network error
                case 1205:        // deadlock
                    return(new TransientErrorException("Temporary db error occured. Try again later.", sqle));

                // case 208: // sql grammar
                // case 547: // constraint violation
                // case 3960: // stale object state (does not occur with ReadCommitted isolation level). Should trigger reload on client side
                default:
                    return(new ApplicationException("DB error", sqle));
                }
            }
            return(SQLStateConverter.HandledNonSpecificException(exInfo.SqlException,
                                                                 exInfo.Message, exInfo.Sql));
        }