1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activejob/test/jobs/rescue_job.rb
Étienne Barrié 142ae54e54 Allow jobs to rescue all exceptions
Before this commit, only StandardError exceptions can be handled by
rescue_from handlers.

This changes the rescue clause to catch all Exception objects, allowing
rescue handlers to be defined for Exception classes not inheriting from
StandardError.

This means that rescue handlers that are rescuing Exceptions outside of
StandardError exceptions may rescue exceptions that were not being
rescued before this change.

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
2021-01-23 08:35:51 -05:00

37 lines
956 B
Ruby

# frozen_string_literal: true
require_relative "../support/job_buffer"
class RescueJob < ActiveJob::Base
class OtherError < StandardError; end
rescue_from(ArgumentError) do
JobBuffer.add("rescued from ArgumentError")
arguments[0] = "DIFFERENT!"
job = retry_job
JobBuffer.add("Retried job #{job.arguments[0]}")
job
end
rescue_from(ActiveJob::DeserializationError) do |e|
JobBuffer.add("rescued from DeserializationError")
JobBuffer.add("DeserializationError original exception was #{e.cause.class.name}")
end
rescue_from(NotImplementedError) do
JobBuffer.add("rescued from NotImplementedError")
end
def perform(person = "david")
case person
when "david"
raise ArgumentError, "Hair too good"
when "other"
raise OtherError, "Bad hair"
when "rafael"
raise NotImplementedError, "Hair is just perfect"
else
JobBuffer.add("performed beautifully")
end
end
end