Module: Prosody::ErrorClassification

Included in:
EventHandler
Defined in:
lib/prosody/handler.rb,
sig/handler.rbs

Overview

Mixin providing class-level methods to wrap instance methods so that specified exceptions are re-wrapped as PermanentError or TransientError.

Instance Method Summary collapse

Instance Method Details

#permanent(method_name, *exception_classes) ⇒ void

This method returns an undefined value.

Wraps the given instance method so that specified exception types are caught and re-raised as Prosody::PermanentError.

Parameters:

  • method_name

    the name of the method to wrap

  • exception_classes

    one or more Exception subclasses to catch

Raises:

  • (ArgumentError)

    if no exception classes given or invalid

  • (NameError)

    if method_name is not defined



75
76
77
# File 'lib/prosody/handler.rb', line 75

def permanent(method_name, *exception_classes)
  wrap_errors(method_name, exception_classes, PermanentError)
end

#transient(method_name, *exception_classes) ⇒ void

This method returns an undefined value.

Wraps the given instance method so that specified exception types are caught and re-raised as Prosody::TransientError.

Parameters:

  • method_name

    the name of the method to wrap

  • exception_classes

    one or more Exception subclasses to catch

Raises:

  • (ArgumentError)

    if no exception classes given or invalid

  • (NameError)

    if method_name is not defined



87
88
89
# File 'lib/prosody/handler.rb', line 87

def transient(method_name, *exception_classes)
  wrap_errors(method_name, exception_classes, TransientError)
end

#wrap_errors(method_name, exception_classes, error_class) ⇒ void

This method returns an undefined value.

Core implementation: redefines method_name to catch listed exceptions and re-raise them as the specified error class.

Parameters:

  • method_name

    the method to wrap

  • exception_classes

    exceptions to catch

  • error_class

    the error class to wrap caught exceptions in



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/prosody/handler.rb', line 100

def wrap_errors(method_name, exception_classes, error_class)
  # Must specify at least one exception class
  if exception_classes.empty?
    raise ArgumentError, "At least one exception class must be provided"
  end

  # Ensure the method exists (in this class or its ancestors)
  unless method_defined?(method_name)
    raise NameError, "Method `#{method_name}` is not defined"
  end

  # Build a prepended wrapper module
  wrapper = Module.new do
    define_method(method_name) do |*args, &block|
      super(*args, &block)
    rescue *exception_classes => e
      # The new exception's #cause will be set automatically
      Kernel.raise error_class.new(e.message)
    end
  end
  wrapper.instance_variable_set(:@prosody_error_wrapper, true)

  prepend wrapper
end