User7354632781
User7354632781

Reputation: 2274

how to rescue specific exceptions in rails

I am executing a sql function and capturing standard error. is it the right way? if not how can i catch specific exception?

    query = ActionController::Base.helpers.sanitize(sql)
    begin
      result = ActiveRecord::Base.connection.exec_query(query)

    rescue StandardError => e
      raise Programmability::FatalError.new(nil, e.inspect)
    end

Upvotes: 0

Views: 619

Answers (1)

tadman
tadman

Reputation: 211590

You're catching a semi-specific exception. To catch a specific exception change StandardError to the error you're interested in.

You have all the information you need in e, so if you want to know more, Rails.logger.debug(e.class) will add information to log/development.log so you can tune your rescue.

Upvotes: 1

Related Questions