LoginSignup
65

More than 5 years have passed since last update.

exception_notificationでrailsの例外をslackに送る

Last updated at Posted at 2014-09-03

Railsで発生した例外をSlackに送りたい

Gemfile

  • slack対応版が未だgithubのmasterにしか無いのでgithubから取得
gem 'exception_notification', :github => 'smartinez87/exception_notification'
gem 'slack-notifier'

雛形生成

bundle exec rails g exception_notification:install
      create  config/initializers/exception_notification.rb

exception_notification.rb編集

  • ここにはtokenはapi tokenと書いてあるがwebhooksのtokenでOK
  • ActiveRecord::RecordNotFound, AbstractController::ActionNotFound, ActionController::RoutingError はdefaultで監視から外れている様なので必要であればconfig.ignored_exceptions = ''を追加する
 config.add_notifier :slack, {
    :team => "チーム名称",
    :token => "webhooks token",
    :channel => "#グループ"
  }
 config.add_notifier :slack, {
    :webhook_url => "webhooks url",
    :channel => "#グループ"
  }

application_controller.rbの編集

  • application_controller.rbで例外をハンドリングする
  rescue_from Exception, with: :render_500
...
  def render_500(e)
    ExceptionNotifier.notify_exception(e, :env => request.env, :data => {:message => "error"})
    render template: 'errors/error_500', status: 500
  end

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
65