Thursday, 25 September 2008

Surprisingly Good Error Messages!

I was stunned recently by finding a piece of software that does something reasonably complicated, but still manages to give good error messages.

Just look at this beauty from Spring IOC:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'magiceightball' defined in file [C:\...\random\magiceightball.test.xml]: Cannot resolve reference to bean 'randomsource' while setting constructor argument; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [foo.Foo] for bean with name 'randomsource' defined in file [C:\...\random\magiceightball.test.xml]; nested exception is java.lang.ClassNotFoundException: foo.Foo at [...]

That is actually perfectly readable! Alright, I am a nerd, and I have some experience with debugging Java code, but I bet a 9-year-old child could deduce that they forgot to implement a RandomSource called Foo.

I was stunned when I saw it, perhaps because it is so rare to get useful error messages from clever frameworks. As soon as reflection enters the picture, people just give up it seems. Look at this example as a contrast:

com.google.inject.CreationException: Guice configuration errors: 1) Error at foo.BarTestModule$TestBar.(BarTest.java:18): Could not find a suitable constructor in foo.BarTestModule$TestBar. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor. 1 error[s] at com.google.inject.BinderImpl.createInjector(BinderImpl.java:277)
[blah blah]

Hmm, that's funny, I distinctly remember creating a zero-argument constructor in TestBar??? It turns out, Guice does not support injection of inner classes. And it only took me an hour to figure that out... So is this error message helpful, or rather harmful?

Hibernate is equally bad and will give you a lot of grief if you do not tread carefully. And these are popular, high-profile pieces of software, not random open-source amateur junk off of SourceForge. So how come Spring IOC got it right and these guys got it so wrong?

3 comments:

swankjesse said...

This is fixed in Guice SVN.

Bob said...

Re: "So how come Spring IOC got it right and these guys got it so wrong?"

That's a pretty big leap to make from one example. You've encountered one of the very few bugs in Guice 1.0.

On the whole, Guice provides *much* better error reporting than Spring. More examples: http://code.google.com/p/garbagecollected/wiki/SpringAndGuiceErrors

LasseWesth said...

Fair points. My objective wasn't to knock Guice as much as it was to big up Spring IOC error messages - or at least the ones I encountered and found very helpful.

It is a general point though: I work with lots of tools and libraries, and spend a lot of time down the wrong path following some meaningless error message. What is your experience?