I have a Rails 6 application.
class PostsController
rescue_from MyException, :handle_my_exception
around_action :my_around_action
def create
Rails.logger.info "Executing create code"
raise MyException.new("Error Message!")
end
def my_around_action
Rails.logger.info "Executing my_around_action"
yield
ensure
Rails.logger.info "Inside ensure block of my_around_action"
end
def handle_my_exception(e)
Rails.logger.info "Inside Handle My Exception"
end
end
After calling create action I get output like this in below order
Executing my_around_action
Executing create code
Inside ensure block of my_around_action
Inside Handle My Exception
But I want output in this order.
Executing my_around_action
Executing create code
Inside Handle My Exception
Inside ensure block of my_around_action
What should I do? I must have to use rescue_from for dry purpose. Below is what I tried to overcome the problem, but I am looking to solve it using rescue_from.
class PostsController
# rescue_from MyException, :handle_my_exception
around_action :my_around_action
def create
Rails.logger.info "Executing create code"
raise MyException.new("Error Message!")
rescue MyException => e
handle_my_exception(e)
end
def my_around_action
Rails.logger.info "Executing my_around_action"
yield
ensure
Rails.logger.info "Inside ensure block of my_around_action"
end
def handle_my_exception(e)
Rails.logger.info "Inside Handle My Exception"
end
end
When I manually handled exception then it outputs in correct order, but do not output in proper order when I try to use rescue_from.