Hello Everyone!
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.
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'
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:
Before adding the configuration, we have to create a slack channel into which we wanted to receive the exception.
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: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
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
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}
}
So this is the tutorial about to receive the exception so that we can solve the errors before it will become a big mistake.
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