public void ConstructorErrorStack() { ErrorMessageCollection stack = new ErrorMessageCollection(); ConstraintException exception = new ConstraintException(stack); Assert.AreEqual(stack, exception.Errors); }
public void ConstructorMessageInner() { Exception inner = new Exception(); ConstraintException exception = new ConstraintException("Message", inner); Assert.AreEqual("Message", exception.Message); Assert.AreEqual(inner, exception.InnerException); }
public void ConstructorInner() { Exception e = new Exception(); ConstraintException exception = new ConstraintException(string.Empty, "Message", e); Assert.AreEqual("Message", exception.Code); Assert.AreEqual(e, exception.InnerException); }
public void Ctor_ArgumentsRoundtrip() { var innerException = new Exception("inner exception"); var e = new ConstraintException("test", innerException); Assert.Equal("test", e.Message); Assert.Same(innerException, e.InnerException); Assert.Equal(-2146232022, e.HResult); }
public void ConstructorFieldMessage() { ConstraintException exception = new ConstraintException("BEA_ID", "Message"); Assert.IsNotNull(exception.Errors); Assert.AreEqual(1, ((ICollection <ErrorMessage>)exception.Errors).Count); foreach (ErrorMessage entry in exception.Errors) { Assert.AreEqual("BEA_ID", entry.FieldName); Assert.AreEqual("Message", entry.Message); } }
public void Ctor_ArgumentsRoundtrip() { const int COR_E_DataConstraint = unchecked ((int)0x8013192a); var innerException = new Exception("inner exception"); var e = new ConstraintException("test", innerException); Assert.Equal("test", e.Message); Assert.Same(innerException, e.InnerException); Assert.Equal(COR_E_DataConstraint, e.HResult); }
private static Exception HandleConstraintException(ConstraintException ce) { var outputLines = new StringBuilder(); outputLines.AppendLine("The following constraints were violated"); foreach (KeyValuePair <string, string> entry in ce.Data) { outputLines.AppendLine(" Key: " + entry.Key + " , Value: " + entry.Value); } var detailedException = outputLines.ToString(); return(new ContextException(detailedException)); }
private void gridClasses_DataError(object sender, DataGridViewDataErrorEventArgs e) { ConstraintException ex = e.Exception as ConstraintException; if (ex != null) { MessageBox.Show(this, "The name has already been used. Please use a unique Class Name.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); e.ThrowException = false; } else { e.ThrowException = true; } }
public void Unpersist() { Session.BeginUpdate(); Event1 = QueryHelper.Read <Event>(Event1SimpleKey, Location1, Session); Set1 = QueryHelper.Read <Set>(Set1.SimpleKey, Event1, Session); Set2 = QueryHelper.Read <Set>(Set2.SimpleKey, Event1, Session); ConstraintException exception = Assert.Catch <ConstraintException>(() => Session.Unpersist(Event1), "Unpersist Event with Sets"); Session.Unpersist(Set1); Session.Unpersist(Set2); Session.Commit(); Assert.IsTrue( exception.Message.Contains("' cannot be deleted because it is referenced by "), "ConstraintException message"); Session.BeginUpdate(); Event1 = QueryHelper.Read <Event>(Event1SimpleKey, Location1, Session); EventType1 = QueryHelper.Read <EventType>(EventType1Name, Session); Location1 = QueryHelper.Read <Location>(Location1Name, Session); Newsletter1 = QueryHelper.Read <Newsletter>(Newsletter1SimpleKey, Session); Session.Unpersist(Event1); Session.Commit(); Assert.AreEqual(2, EventType1.Events.Count, "EventType1.Events.Count after deleting Event1"); Assert.AreEqual(1, Location1.Events.Count, "Location1.Events.Count after deleting Event1"); Assert.AreEqual(0, Newsletter1.Events.Count, "Newsletter1.Events.Count after deleting Event1"); Session.BeginUpdate(); Event1 = QueryHelper.Find <Event>(Event1SimpleKey, Location1, Session) !; EventType1 = QueryHelper.Read <EventType>(EventType1Name, Session); Location1 = QueryHelper.Read <Location>(Location1Name, Session); Newsletter1 = QueryHelper.Read <Newsletter>(Newsletter1SimpleKey, Session); Session.Unpersist(Event1); Session.Commit(); Assert.IsNull(Event1, "Event1 in new Session"); Assert.AreEqual(2, EventType1.Events.Count, "EventType1.Events.Count in new Session"); Assert.AreEqual(1, Location1.Events.Count, "Location1.Events.Count in new Session"); Assert.AreEqual(0, Newsletter1.Events.Count, "Newsletter1.Events.Count in new Session"); }
public void ConstructorDeserialize() { ConstraintException exception = new ConstraintException((BeanPropertyDescriptor)null, "Message"); BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All)); byte[] buffer = null; using (MemoryStream ms = new MemoryStream()) { formatter.Serialize(ms, exception); buffer = ms.ToArray(); } ConstraintException deserializeException = null; using (MemoryStream ms = new MemoryStream(buffer)) { deserializeException = (ConstraintException)formatter.Deserialize(ms); } Assert.AreEqual("Message", deserializeException.Message); }
public static MyException TranslateException(Exception e, DataGridView dgv) { string s, msg = ""; if (e == null) { throw new ArgumentException(); } if (dgv == null) { return(new MyException(e.Message, e)); } DataTable dt = GetDataTableFromDataGridView(dgv); if (e is SqlException && e.Message.StartsWith(_sqlExceoptionStr1)) { //The UPDATE statement conflicted with the REFERENCE constraint \"fk_OPS_AC11_Acp21_AC\". The conflict occurred in database \"C:\\A1-DOCS\\C_NET\\KLONS1\\DB\\KLONS.MDF\", table \"dbo.OPS\", column 'AC11' SqlException esq = e as SqlException; //s = GetFieldName(esq, table); //s = table.Columns[s].Caption; msg = string.Format("Lauka vērtību nevar mainīt, " + "ja tā ir izmantota sistītā tabulā"); } else if (e is FbException && e.Message.StartsWith("violation of FOREIGN KEY constraint ")) { //violation of FOREIGN KEY constraint "FK_OPSD_CLID_PERSONS_CLID" on table "OPSD" Foreign key references are present for the record Problematic key value is ("CLID" = 'zapte') msg = string.Format("Darbību ar ierakstu nevar izpildīt, " + "ja tas ir izmantots sistītā tabulā"); } else if (e is DBConcurrencyException) { msg = "Izmaiņas neizdevās saglabāt,\n" + "jo ierakstu ir labojis cits lietotājs.\n" + "Mēģiniet pārlasīt tabulas datus."; } else if (e is NoNullAllowedException) { NoNullAllowedException enn = e as NoNullAllowedException; s = GetFieldName(enn); s = GetColumnDescr(dgv, s); msg = string.Format("Lauks [{0}] nevar būt tukšs", s); } else if (e is ConstraintException && e.Message.StartsWith("Column '") && e.Message.Contains("is constrained to be unique")) { //Column 'AC' is constrained to be unique. Value '1210' is already present. ConstraintException ec = e as ConstraintException; s = GetFieldName(ec); s = GetColumnDescr(dgv, s); msg = string.Format("Nekorekta lauka [{0}] vērtība" + " (šāda vērtība tabulā jau ir).", s); } else if (e is ConstraintException && e.Message.StartsWith("Failed to enable constraints. One or more rows contain")) { //Failed to enable constraints. One or more rows contain DetailedConstraintException de = new DetailedConstraintException(e.Message, GetDataTableFromDataGridView(dgv), e); msg = de.Message; } else if (e is InvalidConstraintException && e.Message.IndexOf("deleting") >= 0) { msg = "Ierakstu nevar dzēst, jo tam ir saistīti ieraksti citā tabulā."; } else if (e is InvalidConstraintException) { InvalidConstraintException eic = e as InvalidConstraintException; s = GetFieldName(eic, dt); s = GetColumnDescr(dgv, s); msg = string.Format("Nekorekta lauka [{0}] vērtība", s); } else if (e is ArgumentException && e.Message.Contains("The value violates the MaxLength limit")) { //ArgumentException: Cannot set column 'ClId'. The value violates the MaxLength limit of this column s = GetFieldName(e as ArgumentException); s = GetColumnDescr(dgv, s); msg = string.Format("Lauka [{0}] teksts ir par garu.", s); } else if (e is FormatException) { //Input string was not in a correct format. msg = string.Format("Ievadīta nekorekta lauka vērtība\n" + "(nekorekts ievadītā teksta formāts, ...)."); } if (msg == "") { msg = e.Message; } return(new MyException(msg, e)); }
public void AfterThrowing(ConstraintException ex) { var a = 0; }
public Exception ProcessException(Exception innerException, DbConnection connection, string commandText, DbTransaction transaction) { Logger.LogException(innerException); if (innerException is SqliteException ex) { SQLException sqlEx; #if MICROSOFT_DATA_SQLITE var errorCode = (SqliteResultCode)ex.SqliteErrorCode; #elif SYSTEM_DATA_SQLITE var errorCode = (SqliteResultCode)ex.ErrorCode; #endif switch (errorCode) { case SqliteResultCode.Corrupt: case SqliteResultCode.NotADb: case SqliteResultCode.Perm: case SqliteResultCode.IoErr: case SqliteResultCode.CantOpen: case SqliteResultCode.Full: case SqliteResultCode.Auth: { return(new FileAccessException(errorCode.ToString(), innerException)); } case SqliteResultCode.ReadOnly: { sqlEx = new ReadOnlyException(null, innerException); break; } case SqliteResultCode.Locked: { sqlEx = new ConnectionException("file is locked", ex); break; } case SqliteResultCode.Constraint: { if (innerException.Message.IndexOf("UNIQUE constraint failed", StringComparison.OrdinalIgnoreCase) >= 0) { sqlEx = new UniqueConstraintException(null, innerException); } else { sqlEx = new ConstraintException(null, innerException); } break; } default: { sqlEx = new SQLException(null, innerException); break; } } if (connection != null) { try { sqlEx.ConnectionString = connection.ConnectionString; sqlEx.ConnectionState = connection.State.ToString(); } catch (ObjectDisposedException) { sqlEx.ConnectionState = "Disposed"; } } if (connection != null) { sqlEx.CommandText = commandText; //newEx.Data.Add("CommandText", comm.CommandText); } return(sqlEx); } #if MICROSOFT_DATA_SQLITE else if ((innerException is InvalidOperationException ioex) && ioex.Source == "Microsoft.Data.Sqlite") { return(new SQLException(null, innerException) { CommandText = commandText }); }
private static string GetFieldName(ConstraintException e) { return(GetFieldNameSimple(e.Message)); }
public void ConstructorPropertyMessage() { ConstraintException exception = new ConstraintException((BeanPropertyDescriptor)null, "Message"); Assert.AreEqual("Message", exception.Message); }
protected override Exception ThrowExceptionHelper(DbConnection conn, DbCommand comm, Exception innerException) { if (innerException is SQLiteException) { SQLException sqlEx; var ex = innerException as SQLiteException; #if Mono var errorCode = ex.ErrorCode; #else var errorCode = ex.ResultCode; #endif switch (errorCode) { case SQLiteErrorCode.Corrupt: #if Mono case SQLiteErrorCode.NotADatabase: #else case SQLiteErrorCode.NotADb: #endif case SQLiteErrorCode.Perm: #if Mono case SQLiteErrorCode.IOErr: #else case SQLiteErrorCode.IoErr: #endif case SQLiteErrorCode.CantOpen: case SQLiteErrorCode.Full: case SQLiteErrorCode.Auth: { return new FileAccessException(errorCode.ToString(), innerException); } case SQLiteErrorCode.ReadOnly: { sqlEx = new ReadOnlyException(null, innerException); break; } case SQLiteErrorCode.Locked: { sqlEx = new ConnectionException("file is locked", ex); break; } case SQLiteErrorCode.Constraint: { if (innerException.Message.IndexOf("UNIQUE constraint failed", StringComparison.OrdinalIgnoreCase) >= 0) { sqlEx = new UniqueConstraintException(null, innerException); } else { sqlEx = new ConstraintException(null, innerException); } break; } default: { sqlEx = new SQLException(null, innerException); break; } } if (conn != null) { try { sqlEx.ConnectionString = conn.ConnectionString; sqlEx.ConnectionState = conn.State.ToString(); } catch (ObjectDisposedException) { sqlEx.ConnectionState = "Disposed"; } } if (comm != null) { sqlEx.CommandText = comm.CommandText; //newEx.Data.Add("CommandText", comm.CommandText); } return sqlEx; } else { return new ORMException(null, innerException); } }
public void ConstructorMessage() { ConstraintException exception = new ConstraintException("Message"); Assert.AreEqual("Message", exception.Message); }
public void ConstructorDefault() { ConstraintException exception = new ConstraintException(); }