Rails find with Airbrake

When creating a rails application, it’s common to use a service like Airbrake that will alert you that something has gone wrong on your applcation. Airbrake is a great utility, but we found a slight gotcha when relying on it to report errors.

If we had a pages resource, we might be able to access a specific page by going to /pages/10. Friendly URLs are common and create a better experience for the user. However, a user might poke around with the URL and type in /pages/1-bajillion. If this page did not exist, it’s unlikely that you would care to know that this happened, since it was generated by a user mucking with the URL. Airbreak, by default, ignores errors this type of error and considers them to be 404s.

This example assumes that your show page is using the ActiveRecord.find(:id) method, which returns ActiveRecord::RecordNotFound when given an id that does not exist. Other methods, like find_by_id(:id) return nil when the record is not found, forcing you to handle a missing record within your application logic or causing a different type of error to be raised. For most applications, this behavior of RecordNotFound being ignored by AirBrake is desired.

Recently we ran into an area of the application where we were using the find call to access a record. In our case, we received the ID from a database association and not over the URL. This caused a RecordNotFound to be raised and a 404 page to be shown to the user without alerting us that our data had inconsistancies.

We should have either used a find_by_id and handled the case where we recieved a nil record, or we should have caught the RecordNotFound exception in our code.

This small issue caused a lot of 404s to be shown to the users and left our application broken without us knowing it was in this state. From here on out, I’ll be more careful when using a find in an app that runs with Airbrake.