Some Guy
Some Guy

Reputation: 13568

In Rails, can I throw an exception but keep going?

I have something in Rails that processes a ton of events. All events are independent of each other. If one of the events is malformed or whatever, right now it blows up, and stops the processing of events after it.

I want to instead try/catch, then propagate the error to all our various error tracking services (New Relic, logs, etc) with a stack trace and so on. Is there a good way to do this?

I essentially want it to act as if the error wasn't caught, for tracking, but to keep going, for event processing.

Upvotes: 0

Views: 1643

Answers (2)

infused
infused

Reputation: 24337

Yes, this is very possible. For example, let's process a bunch of events. We'll rescue all errors that are descendants of StandardError, log them to honeybadger.io, and continue processing the remaining events:

events.each do |event|
  begin
    process(event)
  rescue StandardError => error
    Honeybadger.notify(error)
  end
end

Upvotes: 1

Brad Werth
Brad Werth

Reputation: 17647

You can rescue the error, pass it along to NewRelic, and continue on your merry way...

def my_method
  # do things...
rescue => exception
  NewRelic::Agent.notice_error(exception)
end

Upvotes: 0

Related Questions