0

私のRORプロジェクトには、エラーが呼び出し元に伝播される前に、常に例外をキャッチしてクリーンアップを実行するコントローラーがあります。これはRORで実行できますか?コントローラで例外が発生したときに呼び出されるフックが必要です。

4

2 に答える 2

1

を使用できますaround_filter

class PagesController < ApplicationController

  around_filter :custom_handle_exception

  def show
    # ...
  end

  private

  def custom_handle_exception
    yield
  rescue StandardError => e
    handle_the_error(e)
    raise e
  end

end

rescue_fromクラスメソッドで同様のことを行うこともできます。

通常、すべての例外を救済するべきではありません。ただし、継承する例外StandardErrorは問題なく救済できるはずです。

于 2013-02-02T00:42:00.227 に答える
1

あなたが使用することができますrescue_from

  class WhateverController < ApplicationController
      rescue_from Exception do |exception|
        # whatever handling here
      end

      # ...
    end
于 2013-02-02T00:49:37.197 に答える