ActiveJob custom `retry_on` logic fails with `NoMethodError: undefined method 'cause' for SomeError:Class`

I’m not sure if this is a bug or a documentation issue. The issue is that the second argument yielded to a custom Active Job retry_on block is the exception class and not the error instance:

class SomeJob < ActiveJob::Base retry_on SomeError, attempts: 4 do |job, exception| logger.error “Job failed: #{exception.cause}” end

def perform # do something which causes an error rescue raise SomeError end end

``

According to the method description the second parameter is supposed to be:

the error instance as the second parameter

However, slightly further down the code example uses exception which mirrors the exception class provided to retry_on:

retry_on(YetAnotherCustomAppException) do |job, exception| ExceptionNotifier.caught(exception) end

``

This is in fact what is yielded to the block; the exception class not the error instance.

Is this intended design or an oversight in the initial implementation. I don’t find the exception class to be overly useful within the block, compared to the error instance.

Hey, nice catch!

Think we should raise the error instead of the exception here: https://github.com/rails/rails/pull/25991/commits/9d8d4ee05e08b928dbb25bb14e49ea28f30d14c6#diff-59beb0189c8c6bc862edf7fdb84ff5a7R50

That would match rescue_from which retry_on is a veneer on top of.

Do submit a PR :+1:

Looks like this is already in Rail 5.2. It was fixed on master back in Oct. 2017 in Yield with an error instance instead of error class by k2nr · Pull Request #30750 · rails/rails · GitHub. :smiley:

Little late for an April fools there, Aaron :smile:

I guess we both should learn to check the master branch :innocent: