How to add exception notification in ruby on rails using the exception notifies gem

in #utopian-io6 years ago (edited)

Repository

Exception Notification

What Will I Learn?

  • You will learn how to install the gem in ruby on rails
  • You will learn how to configure ruby on rails app using exception notification gem to receive exception notifications
  • You will learn how to receive an exception on the slack app and email

Requirements

  • OS: Ubuntu/Mac OS
  • A text editor like sublime
  • Basic understanding of Ruby on Rails

Difficulty

  • Intermediate

Tutorial Contents

Hello Everyone!

Why we use Exception notifier?

When a web app is live and we have done all the testing from our side but still some cases are left and they have found on a user side. So how we know that something is wrong with our web app, sometimes user send us feedback but mostly the time for a new product, a user just stops using it or sometimes they get irritated from the errors. The best practice is to add an exception notifier in our web app. so whenever an internal server error (error 500) occurs in our web app, the notifier send us a notification on slack/email so that we can fix that error asap.

Tutorial

Open the rails project that was built in my previous tutorials (please refer the links added in the curriculum to see my previous tutorials) so that you can better understand it.

First, we have to add exception libraries into the app. Open the gemfile add the following gem,

# exception alert
gem 'exception_notification'
gem 'slack-notifier'
exception notification: This gem is used to handle the exception
slack notifier: This gem is used to send webhooks to slack app

Now install the library into the app by simply run the following command at the terminal under project path.

bundle install

You can also verify the gem is install or not by running the following command in the terminal under the project path:

bundle show

We are going to receive exception notification on following platforms:

Exception on Slack App

Before adding the configuration, we have to create a slack channel into which we wanted to receive the exception.

Steps to create the slack channel and setup for incoming webhooks

  • Login your slack account, if you do not have the slack account then please create an account first on slack
  • After login go to the browse app section and select the incoming webhooks option, please refer the below screenshot

  • After entering into the incoming webhook app, now select add configuration option, see reference screenshot

  • A new page will open, where you will see Post to channel section. Now enter the channel name where you want your exception messages or simply create a new one, see reference screenshot

  • After selection the add incoming webhook integration option, a new page will open which includes all the settings. Now copy the webhook URL from there and paste it into your notes. we will require this URL later and also save the configuration. After saving it go to your channel see a new message says 'user added an integration to the channel: incoming-webhooks' which means the selected channel is successfully integrated to the webhook, if you did not receive this message then please verify all the steps, see references screenshot below:

Now we go to the project and open development.rb file of your project add the following code

   Rails.application.config.middleware.use ExceptionNotification::Rack,
  :slack => {
    :webhook_url => "channel webhook url",
    :channel => "#testing",
    :additional_parameters => {
      :mrkdwn => true
    },
  }

The above code will help to send webhooks whenever an exception occurs in ruby on rails app. It uses the middleware configuration of the app and calls the ExceptionNotification::Rack module which is defined in exception notifier gem. It uses the slack app to send the error which has:
webhook URL: our channel webhook incoming URL (that url which was saved in our notes before
channel: Channel name into which you want to see the errors
markdown: It accepts the markdown text

Testing webhooks

Now we have successfully integrated the exception notifier, so now it's time to test it. For this, we have to create an exception. You already know that our app root URL is custom controller home path , so add some wrong code there. Go to the controller > users > custom controller and edit the home method. Add some unnecessary code here like

class Users::CustomController < ApplicationController
  def resource_name
    :user
  end
def home
    @error.first
end

end
It will find the first element from error variable but it will return undefind method first for nil class because it will try to get the first element but error variable is also blank and we can not get the first element from a blank object.

Now start the rails server:

rails s 

And go to localhost:3000 in the browser, it will give you an error. Now, check you will definitely receive an incoming webhook in the selected channel. see reference screenshot

Receiive exception Notification using email

If you want to receive all the exception into your email the simply go to development.rb and use the below code:


  Rails.application.config.middleware.use ExceptionNotification::Rack,
  :ignore_if => ->(env, exception) { exception.message =~ /^undefined method `include?' for nil:NilClass/ },
  :email => {
     :deliver_with => :deliver,
     :email_prefix => "[Rails Demo 500 Error] ",
     :sender_address => %{'Rails Demo' <your email address>},
     :exception_recipients => %w{receipient emial address}
   }
It will use the same middleware configuration to send the exception but incoming parameters are diffrent
ignore if: It will check if exception message is equal to your condition, then it ignores the exception. It will help where you do not want to receive an exception
email prefix: Is the subject of the email
sender address: From which email address the exception notifier sends the exception. This address is the same address that will use in the email configuration
exception recipient: It refers to those users which will receive the exception in the form of an email.

NOTE:

  • To receive the exception in the email, you do not want to install the slack notifier gem
  • we refer here development.rb file to add configuration for the exception because we are in development environment but when you are using the staging environment then the same configuration will be added in staging .rb file and same for the live app, the configuration will be added in production.rb file.

So this is the tutorial about to receive the exception so that we can solve the errors before it will become a big mistake.

Curriculum

https://steemit.com/utopian-io/@amn/how-to-add-email-confirmation-while-sign-up-using-devise-in-ruby-on-rails
https://steemit.com/utopian-io/@amn/how-to-upload-images-in-ruby-on-rails-using-paperclip
https://steemit.com/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part3
https://steemit.com/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part2
https://steemit.com/utopian-io/@amn/how-to-authenticate-user-in-rails-using-devise-part1

Proof of Work Done

https://github.com/aman9463/Rails-Demo

Sort:  

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend few advices for your upcoming contributions:

  • Put more resources.
  • There are parts of the code that have little explanation, try to explain as much as possible.
  • Improve the structure of the tutorial.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you so much @portugalcoin for the appreciation. I will continue this contribution and also I will improve the tutorial structure and put more content in upcoming tutorials.

Hey @portugalcoin
Here's a tip for your valuable feedback! @Utopian-io loves and incentivises informative comments.

Contributing on Utopian
Learn how to contribute on our website.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

You have a minor misspelling in the following sentence:

### Recieve exception Notification using email.
It should be receive instead of recieve.

Hey @amn
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Contributing on Utopian
Learn how to contribute on our website or by watching this tutorial on Youtube.

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thank you so much

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by amn from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

Coin Marketplace

STEEM 0.21
TRX 0.20
JST 0.033
BTC 97859.91
ETH 3296.28
USDT 1.00
SBD 3.01