RoR – Get your exceptions notified via Slack

Development
RoR – Get your exceptions notified via Slack

Have you ever wanted to be one step ahead, and be notified of any unwanted exception before your users have to deal with it? If so, read this article to configure your rails application in just a few steps.


First, we are going to need two gems:

  • Exception Notification. It provides a set of notifiers for sending notifications when errors occur in a Rack/Rails application.
  • Slack-Notifier. A simple wrapper to send notifications to Slack webhooks.

Install them as you would do with any other gem. In my case, I add them to the Gemfile and then run bundle install. Setting them up is as easy as adding a few lines in our environment files, or an initializer config file:


Rails.application.config.middleware.use ExceptionNotification::Rack,
     :slack {
       :webhook_url = “YOUR_WEB_HOOK_URL”
     }


But wait, how do I get that YOUR_SLACK_WEB_HOOK_URL?
  1. Go to https://slack.com/apps/A0F7XDUAZ-incoming-webhooks
  2. Once you have logged in into your team, on the left-side panel, choose “Add Configuration.”
  3. Select the Slack channel where you want to get notified, and then press “Add Incoming WebHooks integration.”
  4. You are going to see a Webhook URL like https://hooks.slack.com/services/XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX
OK, that was easy, but how can I test that it is working?

Maybe your code is so good that it doesn’t raise an exception. In that case, we can add a rails route to test this functionality. Go to your routes.rb file and add this:

# Route for testing Exception Notification configurationget “test_exception_notifier” => “application#test_exception_notifier”

Then, in your application controller:

def test_exception_notifier
  raise “Test Exception. This is a test exception to make sure the exception notifier is working.”
end

Finally, visit yourhost/test_exception_notifier and voila! You should receive a slack notification looking something like this:

Slack notification example
Slack notification example

In the message, you are going to get a full detail of the exception, including a description, the host’s name, and a full backtrace.

Let us know how this worked for you in the comments!

We are a a Clutch Champion for 2023!
Development
We are a a Clutch Champion for 2023!
Kreitech has been recognized as a 2023 Clutch Champion by Clutch, the leading global marketplace of B2B service provider
Read More
Navigating a Post-2020 World: A Time of Business Challenges and Opportunities
Development
Navigating a Post-2020 World: A Time of Business Challenges and Opportunities
2020 will be remembered as an unprecedented, challenging, and unique year, a significant opportunity for the global
Read More
How Hiring an External Team can Accelerate Development and save costs?
Development
How Hiring an External Team can Accelerate Development and save costs?
In today's fast-paced software industry, companies often face the challenge of meeting deadlines and achieving goals
Read More
Have a project or an idea?
Let's make it real!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.