improve message of InvalidStateException
Description
Environment
Activity
CBP January 11, 2012 at 4:44 AM
I would like to see this implemented too. It is a hassle not seeing what caused validation to fail.
For example, when running tests, logging exception etc. I don't want to have to catch this specific exception every time, just to see this critical piece of information.
korkless March 11, 2011 at 12:28 AM
for me it's always better to have too much info than too few, also becouse if you use this method you expect that the entity is valid or that the invalid values are few or will be better if you use the Validate method.
anyhow my problem is that i was gettin this exception with general unhandled exception logging and i don't like to do specific refactoring of this exception message before log it to have enough info to be usefull, if this is the only way i will do so
Dario Quintana March 10, 2011 at 4:10 PM
It could mess up in entities with a lot of validation errors. For those who want more information should catch the exception, and iterate thru GetInvalidValues() method.
at the moment the message of the exception contains only the class name, i suggest to add also same info
about the invalid values. there is my suggestion
public class InvalidStateException : HibernateException
{
....
public InvalidStateException(InvalidValue[] invalidValues, String className)
: base(FormatMessage(invalidValues, className))
....
private static string FormatMessage(InvalidValue[] invalidValues, String className)
{
StringBuilder msg = new StringBuilder("validation failed for " + className + ":");
foreach(InvalidValue item in invalidValues)
{
if (!string.IsNullOrEmpty(item.PropertyName))
{
msg.Append(item.PropertyName + ":");
}
msg.AppendLine(item.Message);
}
return msg.ToString();
}
}