Get exception notifications on Slack in Rails 4

exception_notification gem provides support to send notifications to development team when an exception occurs in Rails application.

The built-in notifiers can deliver notifications by email, Campfire, HipChat, Slack, Mattermost, IRC or via custom WebHooks.

This tutorial explains how user can use Slack with exception_notification as a notifier.

Sample Application

Prerequisite

  • Create a channel named “exceptions”(or other relevant name) in your Slack account
  • Get Web Hook URL for your Slack account — click here

Add below listed gems in your Gemfile

gem ‘exception_notification’
gem ‘slack-notifier’

Install bundle

bundle install

Integrate notifier in your app

  • Add below code in file /config/environments/staging.rb
config.middleware.use ExceptionNotification::Rack,  
slack: {
webhook_url: "https://YOUR-SLACK-WEB-HOOK-URL",
channel: "#exceptions",
username: "staging-exception", # ENV based username to distinguish Staging Exceptions in channel
additional_parameters: {
icon_emoji: ":large_blue_circle:", # BLUE CIRCLE icon to distinguish Staging Exceptions in channel
mrkdwn: true
}
}
  • Add below code in file /config/environments/production.rb
config.middleware.use ExceptionNotification::Rack,  
slack: {
webhook_url: "https://YOUR-SLACK-WEB-HOOK-URL",
channel: "#exceptions",
username: "production-exception", # ENV based username to distinguish Production Exceptions in channel
additional_parameters: {
icon_emoji: ":red_circle:", # RED CIRCLE icon to distinguish Production Exceptions in channel
mrkdwn: true
}
}
  • Replace YOUR-SLACK-WEB-HOOK-URL with your account’s web-hook url
  • Since we’re using single channel to receive Exception notifications for both Staging & Production environment, we need some identifiers to know the environment in which exception has occurred.
  • Hence, as you can see in above code snippets, we’ve used different values for username and icon_emoji for Staging & Production environment.
  • Alternatively you can also use two different channels with relevant name (i.e, staging-exceptions, production-exceptions)