#91
Custom Error Pages with Slack Notification
7-30-2017
Summary
Learn to use custom error pages to provide a similar look to your application. Get notified on Slack as errors occur to act on them before they're reported.
10
rails
api
error
background processing
9:06
Summary
Learn to use custom error pages to provide a similar look to your application. Get notified on Slack as errors occur to act on them before they're reported.
10
Resources
Slack-notifier Gem - https://github.com/stevenosloan/slack-notifier
Source - https://github.com/driftingruby/091-custom-error-pages-with-slack-notification
Summary
config/environments/development.rbconfig.consider_all_requests_local = falseconfig/application.rbconfig.exceptions_app = self.routesconfig/routes.rbmatch "/404", to: "errors#not_found", via: :all
match "/500", to: "errors#internal_server_error", via: :allerrors_controller.rbclass ErrorsController < ApplicationController
# layout 'errors'
def not_found
end
def internal_server_error
begin
exception = request.env['action_dispatch.exception']
message = exception.message.to_s
source_extract = exception.source_extract.join("\n")
backtrace = exception.backtrace[0..9].join("\n")
SlackNotifyJob.perform_later(message, source_extract, backtrace)
ensure
# head :internal_server_error
render status: 500
end
end
endGemfilegem "slack-notifier"Terminalrail g job SlackNotifyjobs/slack_notify_job.rbclass SlackNotifyJob < ApplicationJob
queue_as :default
def perform(error_message, source_extract, backtrace)
error = "Error: 500 - Internal Server Error"
message = ""
message << "*#{error}*\n"
message << "*Date:* #{Time.now}\n"
message << "*Error:* ```#{error_message}``` \n"
message << "*Source:* ```#{source_extract}``` \n"
message << "*Backtrace*: ```#{backtrace}``` \n"
notifier = Slack::Notifier.new Rails.application.secrets.slack_url
notifier.ping message, username: '091Error', channel: '#general'
end
end
Awesome, I could really use this.
Thanks!
Nice video, thank you :-)
This pattern is extremely error-prone. There was once a section for implementing custom error pages in the official Rails guides that basically suggested the same approach as this episode, which was later removed because of potential issues it could introduce. In this episode specifically, there is a number of potential issues:
A much better way would be to use a service like Sentry and configure it to send events to your slack channel.
Getting this, it is not matching 404
ActionController::RoutingError: No route matches [GET] "/j"Any ideas?
Cheers