dennismonsewicz
dennismonsewicz

Reputation: 25542

Rails 3 rescue_from, with and working with custom modules

I am writing a Rails app that I am wanting to DRY up just a tad bit and instead of calling my custom error class at the top of each controller I need it in, I placed it inside of a Module and just included that module.

Working code (Module):

module ApiException
  class EmptyParameter < StandardError
  end
end

Working 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 :with command, how would I call a method inside my custom module?

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

Upvotes: 3

Views: 1855

Answers (1)

Jeremy Woertink
Jeremy Woertink

Reputation: 21

You could try something like this:

rescue_from EmptyParameter do |exception|
  EmptyParameter.custom_class_method
end

Upvotes: 2

Related Questions