public ChangeScriptFailedException(DbException cause, ChangeScript script, int statement, string executedSql) : base(cause) { this.script = script; this.statement = statement; this.executedSql = executedSql; }
public static string FormatMessage(DbException ex, SqlMigrationRun sqlRun) { string errorMsg = string.Empty; if (ex is SqlException) { var sqlEx = ex as SqlException; var no = sqlEx.Number; switch (no) { case 515: errorMsg = "把一个可空字段变更为非空字段时,由于存在不可空约束,所以数据库该表中不能有该字段为空的数据行。\r\n"; break; case 547: errorMsg = "把一个字段变更为非空外键字段时,由于外键约束,所以数据库该表中不能有该字段数据不满足引用约束的数据行。\r\n"; break; default: break; } } errorMsg += ex.Message; var error = "执行数据库迁移时出错:" + errorMsg; if (sqlRun != null) { error += Environment.NewLine + "对应的 SQL:" + sqlRun.Sql; } return error; }
public void Handle( DbException dbException, String sql, IEnumerable<IParameterStub> parameterStubs ) { var improvedExceptionMessage = _exceptionMessageFormatter.Format( dbException, sql, parameterStubs ); var DbEzException = new DbEzException( improvedExceptionMessage, dbException ); DbEzException.Data.Add( "SQL", sql ); DbEzException.Data.Add( "PARAMETERS", parameterStubs ); throw DbEzException; }
/// <summary> /// The handle batch exception. /// </summary> /// <param name = "dbException">The db exception.</param> protected override void HandleBatchException(DbException dbException) { Exception = dbException; var exp = dbException as SqlException; if (exp != null) { foreach (SqlError error in exp.Errors) { Messages += string.Format("Error line {0}: {1}.{2}", error.LineNumber, error.Message, Environment.NewLine); } } }
public string ExtractSqlState(DbException sqle) { string sqlState; Exception nested; sqlState = ExtractSingleSqlState(sqle); nested = sqle.InnerException; while (sqlState.Length == 0 && nested != null) { if (nested is DbException) { sqlState = ExtractSingleSqlState(sqle); } nested = sqle.InnerException; } return sqlState; }
public static void SqlError(System.Data.Common.DbException ex, string TSql, IDataParameterCollection Parameters) { if (Parameters == null || Parameters.Count == 0) { SqlError(ex, TSql); } string ptr = string.Empty; foreach (IDataParameter p in Parameters) { ptr += string.Format("declare {0} {2}; set {0} ='{1}'\r\n", p.ParameterName, p.Value, p.DbType); } ptr += "-----auto code------\r\n"; ptr += TSql; SqlError(ex, ptr); }
/* Many drivers provide both SqlState and NativeError in the Exception * Some of them, like OdbcException, have fields SQLState, NativeError * Some of them contain it in Data field, like PsqlException * Some of them have only text message */ public int ExtractErrorCode(DbException sqle) { int errorCode; Exception nested; errorCode = ExtractSingleErrorCode(sqle); nested = sqle.InnerException; while (errorCode == 0 && nested != null) { if (nested is DbException) { errorCode = ExtractSingleErrorCode(sqle); } nested = sqle.InnerException; } return errorCode; }
/* OdbcException, OleDbException, IfxException, Db2Exception, and possible others * have Errors collection which contains fields: NativeError and SQLState * These fields can be extracted using reflection */ public override int ExtractSingleErrorCode(DbException sqle) { System.Type type; PropertyInfo pi; int nativeError; type = sqle.GetType(); pi = type.GetProperty("Errors"); if (pi == null) // there is no Errors property { return 0; } nativeError = 0; foreach (object o in (pi.GetValue(sqle, null) as IEnumerable)) { pi = o.GetType().GetProperty("NativeError"); if (pi == null) return 0; nativeError = (int)pi.GetValue(o, null); if (nativeError != 0) break; } return nativeError; }
public override string ExtractSingleSqlState(DbException sqle) { System.Type type; PropertyInfo pi; string sqlState; type = sqle.GetType(); pi = type.GetProperty("Errors"); if (pi == null) // there is no Errors property { return null; } sqlState = ""; foreach (object o in (pi.GetValue(sqle, null) as IEnumerable)) { pi = o.GetType().GetProperty("SQLState"); if (pi == null) return null; sqlState = (string)pi.GetValue(o, null); if (sqlState.Length != 0) break; } return sqlState; }
public override bool IsUniqueViolation(DbException ex, string keyName = "") { throw new NotImplementedException(); }
public override bool IsDbBusy(DbException ex) { throw new NotImplementedException(); }
public static void SqlError(System.Data.Common.DbException ex, IDbCommand cmd) { SqlError(ex, cmd.CommandText, cmd.Parameters); }
public virtual string ExtractConstraintName(DbException sqle) { return null; }
public PrimaryKeyViolation(string message, DbException exception) : base(message, exception) { }
public abstract bool IsDbBusy(DbException ex);
public static void SqlError(System.Data.Common.DbException ex, string TSql) { bool isLocal = false; using (EventLog log = new EventLog("WebException")) { string url, method; string extData; if (System.Web.HttpContext.Current != null) { HttpRequest Request = System.Web.HttpContext.Current.Request; url = Request.Url.ToString(); method = Request.HttpMethod; isLocal = Request.IsLocal; string rp; if (Request.UrlReferrer == null) { rp = "none"; } else { rp = Request.UrlReferrer.ToString(); if (rp.Length > 255) { rp = rp.Substring(0, 255) + "<...>"; } } extData = "\r\nReferrer:" + rp; extData += "\r\nUserAgent:" + Request.UserAgent; if (extData.Length > 512) { extData = extData.Substring(0, 512) + "<...>"; } } else { url = string.Empty; method = "n/a"; extData = string.Empty; } log.Source = "Sql"; StringBuilder msg = new StringBuilder(); msg.AppendFormat("url:{0} {1}", method, url); msg.AppendLine(extData); if (!string.IsNullOrEmpty(TSql)) { msg.AppendLine("Sql:"); msg.AppendLine(TSql); } //msg.AppendLine(ex.Message); if (ex is SqlException) { SqlException msEx = (SqlException)ex; for (int i = 0; i < msEx.Errors.Count; i++) { msg.AppendLine("Message: " + msEx.Errors[i].Message); msg.AppendLine("LineNumber: " + msEx.Errors[i].LineNumber); msg.AppendLine("Source: " + msEx.Errors[i].Source); msg.AppendLine("Procedure: " + msEx.Errors[i].Procedure); } } else { msg.AppendLine("Message:" + ex.Message); msg.AppendLine("ErrorCode:" + ex.ErrorCode); } StackTrace s = new StackTrace(true); for (int i = 1; i < s.FrameCount; i++) { StackFrame t = s.GetFrame(i); MethodBase mb = s.GetFrame(i).GetMethod(); System.Reflection.Module m = mb.Module; int line = t.GetFileLineNumber(); if (mb.Name != "SqlError" && !m.ScopeName.StartsWith("System")) { msg.AppendLine(mb.Name + "() " + t.GetFileName() + " 行:" + t.GetFileLineNumber()); } } //msg.AppendLine(ex.ToString()); byte[] bin = null; if (method == "POST") { Stream stm = System.Web.HttpContext.Current.Request.InputStream; if (stm.Length > 5120) { bin = System.Text.ASCIIEncoding.ASCII.GetBytes("*data length:" + stm.Length.ToString()); } else { long p = stm.Position; bin = new byte[stm.Length]; stm.Seek(0, SeekOrigin.Begin); stm.Read(bin, 0, bin.Length); stm.Position = p; } } try { if (bin != null) { log.WriteEntry(msg.ToString(), EventLogEntryType.Error, 0, 0, bin); } else { log.WriteEntry(msg.ToString(), EventLogEntryType.Error); } } catch { if (isLocal) { throw; } } log.Close(); } }
public virtual bool IsDuplicateException(DbException exception) { var msg = exception.Message.ToUpperInvariant(); return msg.Contains("DUPLICATE") || msg.Contains("UNIQUE"); }
/// <summary> /// Checks is the exception was thrown because an unique constraint was violated. /// Can be used to implement idempotency e.g you can treat primary key violations as duplicate operations /// </summary> /// <param name="db"></param> /// <param name="ex"></param> /// <param name="keyName"></param> /// <returns></returns> public static bool IsUniqueViolation(this DbConnection db, DbException ex, string keyName = null) => db.Provider().IsUniqueViolation(ex, keyName);
public void Handle( DbException dbException, String sql ) { var emptyParams = new IParameterStub[ 0 ]; Handle( dbException, sql, emptyParams ); }
public ForeignKeyViolation(string message, DbException exception) : base(message, exception) { }
/// <summary> /// Extract the name of the violated constraint from the given SQLException. /// </summary> /// <param name="sqle">The exception that was the result of the constraint violation. </param> /// <returns> The extracted constraint name. </returns> public abstract string ExtractConstraintName(DbException sqle);
public override bool ObjectExists(DbException ex, string name = null) { throw new NotImplementedException(); }
public abstract bool ObjectExists(DbException ex, string name = null);
public DbEzException( String message, DbException innerException ) : base(message, innerException) { }
public static void SqlError(System.Data.Common.DbException ex) { SqlError(ex, string.Empty); }
public abstract bool IsUniqueViolation(DbException ex, string keyName = "");
public override bool ObjectExists(DbException ex, string name = null) { if (!ex.Message.Contains("already exists")) return false; if (name.IsNullOrEmpty()) return false; return ex.Message.Contains(name); }
public SchemaRaisedError(string message, DbException exception) : base(message, exception) { }
public override bool IsUniqueViolation(DbException ex, string keyName = "") { if (!ex.Message.Contains("UNIQUE constraint failed:")) return false; if (keyName.IsNullOrEmpty()) return false; return ex.Message.Contains(keyName); }
private void ProcessSQLException(int idx, DbException ex) { log.Error(String.Format("<{0}> exception when processing", idx), ex); }
public abstract string ExtractSingleSqlState(DbException sqle);
public UniqueKeyViolation(string message, DbException exception) : base(message, exception) { }
public DatabaseError(string message, DbException exception) : base(message, exception) { }
/// <summary> /// 基本构造 /// </summary> public AgebullSystemException(System.Data.Common.DbException serr) : this(serr.Message, serr) { }
public abstract int ExtractSingleErrorCode(DbException sqle);