Slim yaw
Slim yaw

Reputation: 79

How to use rails rescue_from outside the controller

is there a way I can use rescue_from outside the controller?

Upvotes: 1

Views: 2166

Answers (1)

mechnicov
mechnicov

Reputation: 15248

Here example for you in plain Ruby as idea using rescue_with_handler

require 'active_support/rescuable'


class MyError < StandardError
end

class MyClass
  include ActiveSupport::Rescuable

  rescue_from MyError, with: :catch_error

  def method_with_error
    raise MyError
    puts "I am after error"
  rescue MyError => e
    rescue_with_handler(e)
  end

  private

  def catch_error
    puts "Catch error"
  end
end

MyClass.new.method_with_error # will print Catch error

Upvotes: 2

Related Questions