Skip to content

Manual exception handling outside of rack

Bjørn Trondsen edited this page Jan 22, 2014 · 7 revisions

You might want to catch exceptions that happens outside of Rack, for instance if you have cron tasks running. These errors will not be caught automatically as no request is fired through Rack.

Let's say you have a created a web shop and you have scheduler that runs once a week to clear out orders that were abandoned before they were confirmed. In the scheduler you wrap the code block with a call to the exception handler like so:

RailsExceptionHandler.catch do
  Order.clear_abandoned
end

The error will be caught and stored if the exception handler is activated (usually only in production). If not, the original error will simply be raised like normal (development, test).