Is it possible to retrieve a detailed error message from a trigger (thrown by SQL Server's RAISERROR) from HibernateException ?
When a SQL server trigger fails, the error message from hibernate looks like this:
Code:
NHibernate.HibernateException: SQL insert, update or delete failed (expected affected row count: 1, actual affected row count: -1). Possible causes: the row was modified or deleted by another user, or a trigger is reporting misleading row count.
at NHibernate.Impl.NonBatchingBatcher.AddToBatch(Int32 expectedRowCount)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Boolean[] notNull, SqlString sql, Object obj, ISessionImplementor session)
at NHibernate.Persister.EntityPersister.Insert(Object id, Object[] fields, Object obj, ISessionImplementor session)
at NHibernate.Impl.ScheduledInsertion.Execute()
at NHibernate.Impl.SessionImpl.Execute(IExecutable executable)
at NHibernate.Impl.SessionImpl.ExecuteAll(IList list)
at NHibernate.Impl.SessionImpl.Execute()
at NHibernate.Impl.SessionImpl.Flush()
at NHibernate.Transaction.AdoTransaction.Commit()
However, it seems difficult to retrieve a more detailed error message even though the trigger raises an error message.
For example, if the end of the trigger looks something like this:
Code:
CREATE myTrigger ON myTable FOR INSERT AS
...
RAISERROR ('Some detailed error message' , 10 , 1 )
ROLLBACK TRANSACTION
Then I would like to be able to do something like this:
Code:
try {
// NHibernate code that will try to insert a row into "myTable"
catch (NHibernate.HibernateException e)
{
log.Debug(e.GetBaseException().ToString());
}
Now, when I look in my logfile I would like to see the following string occurring: "Some detailed error message" but I have not been able to figure out how to do it, and have tested to get more nested exceptions also, such as:
log.Debug(e.GetBaseException().GetBaseException().GetBaseException().GetBaseException().GetBaseException().GetBaseException().GetBaseException().ToString());
But it seems to me as if the error message from the trigger is never forwarded from NHibernate.
Or is it, and then how can I retrieve it ???
(I supect some people might be tempted to suggest not using triggers at all and to put that kind of logic in the C# code instead, but that kind of discussion is not the issue here, and the question here is how to get a more detailed error message from existing stored procedures if it is possible)