Rails 3 rescue_from, with and using custom modules

I am writing a Rails application, I want DRY to be just a little bit, and instead of calling my error class at the top of every controller I need, I placed it inside the module and simply turned on its module.

Work code (module):

module ApiException
  class EmptyParameter < StandardError
  end
end

Work code (controller):

# include custom error exception classes
  include ApiException

  rescue_from EmptyParameter, :with => :param_error

  # rescure record_not_found with a custom XML response
  rescue_from ActiveRecord::RecordNotFound, :with => :active_record_error

    def param_error(e)
      render :xml => "<error>Malformed URL. Exception: #{e.message}</error>"
    end

    def active_record_error(e)
      render :xml => "<error>No records found. Exception: #{e.message}</error>"
    end

Here is my question using the command :with, how can I call a method inside my custom module?

Something like that: rescue_from EmptyParameter, :with => :EmptParameter.custom_class

+3
source share
1 answer

You can try something like this:

rescue_from EmptyParameter do |exception|
  EmptyParameter.custom_class_method
end
+2
source

Source: https://habr.com/ru/post/1781320/


All Articles