0

We're using raven-sentry in our application and I would like to have a default 'catch all' set of extra options applied to all errors.

class StandardError
  def raven_context
    extra = {}

    #add conditional key/values to the hash if they exist
    extra[:account_id] = @account.id if @account

    extra.empty? ? {} : { extra: extra }
  end
end

Because the exception is raising from an instantiated class in either a model or controller, I thought I'd be able to access those variables. If accounts#show raised an exception, I'd want to be able to access @account. If Account.create() raised an exception, I'd like to be able to get some information about that account, like errors.

Is this possible? and if so, how?

TIMBERings
  • 688
  • 1
  • 8
  • 28

1 Answers1

0

You can follow the Rails idea of how to catch/raise the exception such as ActiveRecord::RecordInvalid

So basically you can design a generic error like:

class MyGenericError < StandardError
  attr_reader :account, :others

  def initialize(message = nil, account = nil, others={})
    @account = account
    @others  = others
    super(message)
  end
end

class MySpecificError < MyGenericError; end

So now when you raise an error, just pass the necessary params like:

raise MySpecificError.new('doing something wrong', @account) if doing_something_wrong

Finally, when catching the error, you just get the account attribute and freely use it

Hieu Pham
  • 6,577
  • 2
  • 30
  • 50
  • The only problem here is it'll be errors I specifically raise. With Sentry I can pass all the information through Raven.capture_exception(e, extra: {whatever I want}). Sentry also captures unhandled exceptions, these are the one's I'm particularly interested in. – TIMBERings Sep 27 '16 at 01:00