LoginSignup
66
58

More than 5 years have passed since last update.

rescue_fromの走査は下から上だった。

Posted at

どうも思った通りにrescue_fromが例外を捕まえないと思ったら、下記だった。

They are searched from right to left, from bottom to top, and up the hierarchy.

右から左、下から上へ、そして継承を遡って検索される。

ずっとこの辺りのコードを弄っていなかったのだが、なぜ今まで想定通りに動いていたのだろうか…謎だ。

class ApplicationController < ActionController::Base

  # ...

  # ハンドルしきれなかったエラーは500エラー扱い
  # 評価は右から左、下から上へなされるのでこの場所で良い。
  # @see http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html
  rescue_from Exception, :with => :render_500

  # その他細々したエラーをハンドル
  rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, :with => :render_404
  rescue_from ActiveRecord::RecordNotUnique, :with => :render_409
  rescue_from UnauthorizedError, :with => :render_401
  rescue_from IllegalAccessError, :with => :render_403

  # 500エラーのハンドラ
  def render_500(exception = nil)
    handle_error(exception , 500)
  end

  # ... 他のエラーのハンドラも作る

end
66
58
0

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
66
58