Help us understand the problem. What is going on with this article?

Railsでerrbitにカスタムエラーの通知

More than 3 years have passed since last update.

動機

エラーが起きただけでなく、任意の箇所や、自前の情報を与えてエラー情報をerrbitに通知したい.

方法

Airbrake.notify(exception, opts = {})メソッドを呼ぶ.
第二引数にrequest.env['action_controller.instance'].airbrake_request_dataを指定するとrackからcallする情報と同様の情報を送信可能.

my_controller.rb
def action_in_my_controller
  begin
    MyException.new('An error occured')
  rescue MyException => e
    Airbrake.notify(e, request.env['action_controller.instance'].airbrake_request_data)
  end
end

適当に実装すると上のように書ける.

問題1

requestメソッドを使用するので、controller内でないと利用できないのがネック.

モデルや別のレイヤーからrequestを参照する方法は以下の記事が参考になった.

http://qiita.com/ainame/items/823b396d560d82194d48

問題2

request.env['action_controller.instance'] がnilじゃないことを確認すること.
以下のようなメソッドを用意しておくと良い.

def airbrake_request(env)
  if env['action_controller.instance'].respond_to?(:airbrake_request_data)
    env['action_controller.instance'].airbrake_request_data
  else
    { rack_env: env }
  end
end
finc
健康寿命を伸ばすアプリFiNCの開発・運営を行うモバイルヘルステクノロジーベンチャー
https://finc.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした