Use Slack For Rails Error Notification

Dec 22, 2015 Ruby, Ruby on Rails, Slack

You want to know when your production app encounters an error, right?

Exception Notification works pretty well for trapping errors and sending notifications to your inbox. But let's just say you despise email and you want, instead, to create a feed of error messages in one of your Slack channels.

Well, let's get started.

Exception Notification

First, we'll still use Exception Notification, so let's get that setup. Add the gem to your Gemfile.

Gemfile

gem 'exception_notification'

Install it.

$ bundle install

Then you need to add the config. I tend to make it its own initializer, but feel free to add it where it makes sense to you.

config/initializers/errors.rb

if Rails.env.production?
  Rails.application.config.middleware.use(
    ExceptionNotification::Rack,
    :email => {
      :email_prefix => EMAIL_PREFIX,
      :sender_address => [SENDER_ADDRESS],
      :exception_recipients => [RECIPIENTS]
    }
  )
end

Replace my constants with your values. For example, for EMAIL_PREFIX, I might use '[My App ERROR] '. This becomes the subject of the email. I'll probably send it from something like noreply@myapp.com and to me@myapp.com. Do what makes sense for you.

Also notice that I have wrapped this in an environment conditional. I only care about loading the config when I'm in production.

Next, be sure you have email configured for your production server. I'll assume you already have that working since other functions, like resetting a password, are often done via email (in other words, I'd hope mail is already working on your production server).

At this point, why not deploy and see if it is working? We'll need this to be functional before we move on. I suggest you have a hidden or fairly less common way to create an error that won't affect regular usage for your current users.

IFTTT

If you haven't heard of IFTTT, it's a nice service that follows logic you tell it to perform tasks for you (the "IFTTT" stand for "if this, then that").

Sign up for the service and then let's create a new recipe. Go to My Recipes and click Create Recipe.

Your this is going to be Email. So click this and then type in and select Email.

Next, choose to Send IFTTT an email tagged. The tag can be anything you want. I just use #error.

Your that is Slack, and you want to Post To Channel.

You'll have to authenticate your Slack app first. Then, choose the channel and configure the message however you would like it formatted. Remember you really just have a Subject and Body to work with.

And last, create the recipe.

Reconfigure Exception Notification

Last, you need to update your Exception Notification configuration so it matches IFTTT's specs.

Be sure you do the following:

Make those changes, deploy to production and then give it a whirl (again, that hidden 500 error comes in handy here).

Did you learn something or find this article interesting?

If so, why not