6
Jul 08
0

Error Message or 404?

When building a web site where each page is roughly mapped to a row in your database, what do you do when the requested row is not found? When using Rails, I found the standard method of handling such a situation is to rescue the ActiveRecord::RecordNotFound exception, and display a flash message explaining what happened to the user. When writing a Django application, I found myself raising the Http404 exception, which would render the 404 page, whenever the requested record could not be found. Of course there is no reason why the Rails approach couldn’t be used in a framework like Django. I just simply found it easiest to raise an exception and let the framework handle the rest of the work.

Which is the better approach? Raising an exception to display the 404 page is certainly easy and it also seems most accurate with regard to the meaning of HTTP error pages. But does it provide the right experience for a user? With the Rails approach, a more useful message error message can be displayed on a page that could help a user try to find what he is looking for. This would also help your application crash responsibly, although a 404 is not exactly the same as a crash. Giving the user more information than simply “not found” should improve the experience of using your website. And although I expect you to code perfectly so that the only way to get a 404 is type manually type a bad url into the browser, a good recovery mechanism is well worth the effort.

And don’t forget the MediaWiki approach to pages that are not found: a page is not found because you have not edited it yet!

Leave a comment

(required)

(not published) (required)