Rails のカスタム例外補足時に順番に注意が必要な件

次のようなコードを Rails 4.0 系の controller に記述すると

  rescue_from ActiveRecord::RecordNotFound, with: :render_404
  rescue_from ActionController::RoutingError, with: :render_404
  rescue_from Exception, with: :render_500

内部では ActiveRecord::RecordNotFound が出ている場合でも一番下の Exception を拾う方が優先されて render_500 が実行されてしまいます。下のような順番で記述すれば問題ありません。

  rescue_from Exception, with: :render_500
  rescue_from ActiveRecord::RecordNotFound, with: :render_404
  rescue_from ActionController::RoutingError, with: :render_404