Castle Style Errors
I had to compare different styles of error handling approaches, and I automatically compared the ideal, Castle Style errors, to the nasty, Mystery HRESUTL of the Week.
But what does Castle Style errors means?
Let us take a look at a few of them. When an module that was registered before the MonoRail http module had thrown an exception:
throw new Exception( "An exception happened on Global application or on a module that run before MonoRail's module. " + "MonoRail will not be initialized and further requests are going to fail. " + "Fix the cause of the error reported below.", context.Context.Error);
Or what happens when you are trying to access an ActiveRecord class when you didn't initialize it properly?
String.Format("An ActiveRecord class ({0}) was used but the framework seems not " + "properly initialized. Did you forget about ActiveRecordStarter.Initialize() ?", type.FullName);
And:
String.Format("You have accessed an ActiveRecord class that wasn't properly initialized. " + "The only explanation is that the call to ActiveRecordStarter.Initialize() didn't include {0} class", type.FullName);
The common theme for those errors is that they are:
- Descriptive
- Show all the relevant information
- Suggest a probable solution
- Save time
I added the following exception:
throw new ActiveRecordException(String.Format( "You can't use [Property] on {0}.{1} because {2} is an active record class, did you mean to use BelongTo?", model.Property.DeclaringType.Name, model.Property.Name, propertyType.FullName));
I added that after I spent half an hour trying to figure out why something was wrong. Since then, I keep running into this exception, and then it is a "slap on the head, replace [Property] with [BelongsTo] and move on". And that is me, who wrote this bloody annoying exception that I am supposed to already know and avoid triggering. I can't imagine how many hours this exception alone has saved, just for my team.
In fact, when I gave a talk at DevTeach about MonoRail, I have literally built the entire demo by just following the instructions from the exceptions.
That is good error handling, and that is what I consider as the standard that you should aspire to. Anything less than that is cause for anger, anxiety and angst. Do save yourself the ulcer, put in the best errors you can.
Comments
Agreed. :-)
Indeed - my Friday was wasted due to SharePoint giving completely useless HRESULT errors ... http://blog.goinsane.co.uk/archive/2007/11/04/sharepoint-server-has-a-4gb-database-limit-wss-has-no.aspx
Unfortunately, this is not for all parts of Castle. Last Friday, all my tests failed with this rather mysterious error message:
DeserializeElement cannot process element component
I examined my component elements and they were all fine. By 7pm I went home, somewhat frustrated. This morning I reread my config, and guess what it was:
I forgot components-tags around my single component.
YAAAARRRGGG.
Markus,
Can you submit a patch that would make the error cleaner?
It's already on my (evergrowing) todo-list.
Now it reads:
string message = string.Format(
(Not that my todo-list is empty now, I did it in lunch break instead...)
From my wish list for a better world: NHibernate should have more descriptive error messages.
When can I see Castle Style errors for the Brail compiler? My dreams are filled with:
"INDEX.brail(155,97): BCE0044: Boo.Lang.Compiler.CompilerError: unexpected char: '"'
in my 100 line view :)
Bill,
Touche!
Although, it does give you the source view, which should make it easier.
I absolutely agree.
My best exception text saved me a lot of answers on "why this thing is not working". It was (with real library name changed to xxx):
"Native library for this platform was not found.
There can be two reasons for this exception:
You are trying to run the xxx Demo on the 'real' device. The Demo version of xxx can be installed on emulator only. It does not support any other platform types.
This exception can occur when the Visual Studio fails to deploy the native library required by xxx. We suppose that it is a problem of Visual Studio unmanaged deployment system.
As a workaround, we suggest you to add the xxxlib.dll to your project explicitly, as a content file. The emulator version of this dll can be found in the "[path to xxx]\bin\native\x86\" folder."
Comment preview