Article summary
Some error messages, like C++ template errors, are inscrutable, and some, like the venerable favorite “segmentation fault,” are uselessly vague. But the merely unhelpful messages aren’t the worst. It’s the misleading error messages that take the prize.
I’ve recently found a new candidate for the worst error message. It’s the one Ruby sends when you accidentally call methods on nil:
NoMethodError (undefined method 'some_method' for nil:NilClass)
After using Ruby for a few years, I’m pretty used to this message. But I’ve taken a look at it with fresh eyes recently as I’ve been working with newer developers.
What’s So Wrong with This Message?
If you dig into this error message, you’ll notice three key parts: “NoMethodError,” “some_method,” and “nil:NilClass.”
The first, most obvious one to deal with is “NoMethodError.” Developers who see this for the first time will usually respond with, “Well, obviously, that method didn’t exist. Instead of calling foo, I’ll try bar.”
Except that’s not going to fix things at all, because it’s not the real problem. The real problem is the last thing in the list: the fact that we’re trying to interact with a nil reference.
Read the Whole Error Message
I have a terrible habit of glossing over things, whether it’s paragraphs in a book or terms in an error message. Don’t be like me. The speed of reading quickly and carelessly can be addicting, and it may work most of the time. But sometimes, going too fast makes it easy to miss details.
Stack traces from frameworks can be absurdly deep. Ruby on Rails commonly throws errors with 50+ calls–few of them that would be familiar to you or me.
Still, it’s important to look at the whole message. That 85-call-deep stack trace will mostly reference files you’ve never read… but it’ll almost certainly have one or two lines that do refer to your code. This could include one or two lines of your code that contain the root cause of your problem, more often than not.
Don’t just read the error message.
Don’t just read the top line.
Read the whole thing.
It’d be nice if we could have messages that are clear and pinpoint the relevant information for every possible reader. But that’s not how things are. So next time you see an unfamiliar, big, or intimidating error, slow down and review every part of the diagnostic report. Your first clue is in there. Somewhere.
Tsss…php counters with: Unexpected T_PAAMAYIM_NEKUDOTAYIM