諸行無常

IT色々お勉強中のブログ

ruby例外処理捕捉について(slack)

railsで捉えたいexceptionの種類 library _builtin (Ruby 2.1.0)

raiseの仕方 https://docs.ruby-lang.org/ja/latest/doc/spec=2fcontrol.html#raise

エラーのgem GitHub - smartinez87/exception_notification: Exception Notifier Plugin for Rails

rescue_fromとすることでbeginとかしなくてもそのクラスで起きたエラーを拾ってくれる StandardErrorはちょっと色々広いすぎるので何ですが、、自分でエラーを定義したければlibとかで共通に作る

slack例

  rescue_from StandardError do |exception|
    alert_exception(exception)
  end

  def perform(message, channel='dev')
    hook = Rails.configuration.x.send("slack_#{channel}")
    notifier = Slack::Notifier.new hook, http_options: { open_timeout: 2 }
    notifier.username = channel
    notifier.ping message
 end

  private
    def alert_exception(e)
      ExceptionNotifier.notify_exception(e)
    end