Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ActiveJob, how to catch any exception

The ActiveJob docs for exception handling provide this example for how to perform exception handling within the context of a job:

class GuestsCleanupJob < ActiveJob::Base
  queue_as :default

  rescue_from(ActiveRecord::RecordNotFound) do |exception|
   # Do something with the exception
  end

  def perform
    # Do something later
  end
end

I am using this technique in an application I am building and capturing certain particular exceptions. My question is, how to capture any and all exceptions?

I am capturing various kinds of exceptions and performing the same procedure each way, so I would like to DRY up my code and also, in my current implementation, certain exceptions are being ignored which means in some cases my job fails silently.

How do I capture any generic exception using ActiveJob?

like image 284
metahamza Avatar asked Aug 24 '15 23:08

metahamza


1 Answers

Try this

class GuestsCleanupJob < ActiveJob::Base
  ...

  rescue_from(StandardError) do |exception|
   # Do something with the exception
  end

  ...
end
like image 61
Alex Tonkonozhenko Avatar answered Oct 15 '22 12:10

Alex Tonkonozhenko