Padawan coder

Getting from zero to hero...

Exception Notifications in Production

| Comments

Source: XKCD

The last couple of weeks at Flatiron School, we have been split into groups to work on group projects. Anthony, Eugene, Jane, and I have been working on Handrai.se, an in-class question-workflow-management app. In the process of deploying our group’s app, we’ve come to realise that what works perfectly in development, doesn’t always work quite so smoothly in production, because real-life users don’t behave the way we assume they do in our seed file and development testing.

And unlike in development, in production, Rails errors are completely cryptic and uninformative:

To get a more informative error message, you would need to log into the server, and into the ‘current’ folder and type in: tail -f log/production.log

But what if you don’t personally encounter the error?

Avi suggested using an ‘Exception Notifier’ gem to get automatically notified each time an error is triggered in production.

Exception Notification 123s:

1) Read the updated documentation at https://github.com/smartinez87/exception_notification:
Initially, I was really struggling to work with the Exception Notification gem, because I was reading the old, but SEO-optimised documentation at https://github.com/rails/exception_notification.

I really should have realised that such a popular gem (#2 on Ruby-Toolbox with with > 470k downloads) couldn’t really have its last commit dated 3 years ago.

It was Blake, who pointed out that there was a newer, and much more active, repo:
This repo had much clearer instructions.

2) Configuring ActionMailer:
Exception notification’s instructions assumes that you are using sendmail. We decided to go with mailgun.

You can put the following code in the config/environments/production.rb and/or development.rb files depending on whether you want notifications to be sent out only in production or also in development:

1
2
3
4
5
6
7
8
9
10
11
12
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :authentication => :plain,
    :address => "smtp.mailgun.org",
    :port => 587,
    :domain => "handraise.mailgun.org",
    :user_name => ENV['MAILGUN_LOGIN'],
    :password => ENV['MAILGUN_PASSWORD'],
  }

  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true

3) Protecting your API keys, settings and passwords:
To protect the security of your passwords etc., you can either use the Figaro gem, or do it yourself:
i) Create a .yml file in the config folder, e.g. application.yml
ii) Create global variables in the file, but using a colon (:) instead of an equal sign (=), e.g.:

1
2
MAILGUN_LOGIN    : postmaster@handraise.mailgun.org
MAILGUN_PASSWORD : [password]

iii) Call these variables using ENV[''], as in the codeblock in step #2 above
iv) Put the .yml file in your .gitignore file:

1
2
# Ignore application configuration
/config/application.yml

Debugging with Exception Notifier
Most of Exception Notifier’s error messages were detailed and informative. But one in particular (and our first error in fact!) stumped us:

What isn’t precompiled? This is as cryptic an error message as “We’re sorry but something went wrong”. We initially thought it was an asset precompilation issue, but that wasn’t the case.

Bob understood the error immediately. It was “nil isn’t precompiled”, and he knew this because “isn’t precompiled” was slightly further indented than the rest of the message, which indicated a blank space, and hence, “nil”. Riiiiggght.

Apparently, this error arose because we forgot to include a conditionality in our views, to not render a user photo if a user didn’t have a photo. So this was quite easily fixed once we realised what the error was.

Resources:
1) Exception Notification by Sebastian Martinez
2) SMTP configuration at my classmate Victoria’s blog

Comments