Skip to content

GenerationJob calls missing class method handle_exception #306

@TheRealNeil

Description

@TheRealNeil

Summary

GenerationJob#handle_exception_with_agent_class calls klass.handle_exception(exception) (a class method), but the Rescue concern only defines instance methods. This causes a NoMethodError when a job-level exception is raised.

Steps to Reproduce

Any agent that uses generate_later and encounters an unhandled exception in the job:

class MyAgent < ActiveAgent::Base
  rescue_from StandardError, with: :handle_error

  def handle_error(exception)
    Rails.logger.error exception.message
  end
end

When an exception propagates to the job level, GenerationJob tries to call:

klass.handle_exception exception  # NoMethodError — no such class method

Root Cause

In lib/active_agent/generation_job.rb:43:

def handle_exception_with_agent_class(exception)
  if klass = agent_class
    klass.handle_exception exception  # ← class method call
  else
    raise exception
  end
end

The Rescue concern (lib/active_agent/concerns/rescue.rb) only defines instance methods:

  • handle_exceptions (instance method, different name)
  • process (instance method)
  • exception_handler (instance method)

There is no self.handle_exception class method defined anywhere in the gem.

Current Workaround

Users must define the missing class method themselves:

class ApplicationAgent < ActiveAgent::Base
  def self.handle_exception(exception)
    Rails.logger.error "[#{name}] #{exception.class}: #{exception.message}"
    Rails.logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
  end
end

Suggested Fix

Add a handle_exception class method to the Rescue concern:

class_methods do
  def handle_exception(exception)
    ActiveSupport::Notifications.instrument("rescue_from_callback.active_agent", exception: exception)
    Rails.logger.error "[#{name}] #{exception.class}: #{exception.message}"
    Rails.logger.error exception.backtrace&.first(10)&.join("\n") if exception.backtrace
  end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions