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.

Print | posted on Monday, November 05, 2007 12:06 AM

Feedback


Gravatar

# re: Castle Style Errors 11/5/2007 3:15 AM Evan

Agreed. :-)


Gravatar

# re: Castle Style Errors 11/5/2007 10:08 AM Casey

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


Gravatar

# re: Castle Style Errors 11/5/2007 12:41 PM Markus Zywitza

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.


Gravatar

# re: Castle Style Errors 11/5/2007 1:07 PM Ayende Rahien

Markus,
Can you submit a patch that would make the error cleaner?


Gravatar

# re: Castle Style Errors 11/5/2007 1:16 PM Markus Zywitza

It's already on my (evergrowing) todo-list.


Gravatar

# re: Castle Style Errors 11/5/2007 1:41 PM Markus Zywitza

Now it reads:

string message = string.Format(
"Configuration parser encountered <{0}>, but it was expecting to find " +
"<{1}>, <{2}>, <{3}> or <{4}>. There might be either a typo on <{0}> or " +
"you might have forgotten to nest it properly.",
node.Name, ContainersNodeName, FacilitiesNodeName, ComponentsNodeName, BootstrapNodeName);

(Not that my todo-list is empty now, I did it in lunch break instead...)


Gravatar

# re: Castle Style Errors 11/5/2007 3:53 PM hammett

From my wish list for a better world: NHibernate should have more descriptive error messages.


Gravatar

# re: Castle Style Errors 11/5/2007 4:46 PM Bill Pierce

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 :)


Gravatar

# re: Castle Style Errors 11/5/2007 5:01 PM Ayende Rahien

Bill,
Touche!
Although, it does give you the source view, which should make it easier.


Gravatar

# re: Castle Style Errors 11/5/2007 9:08 PM Andrey Shchekin

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:
1. 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.
2. 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."

Comments have been closed on this topic.