Module: Axn::Core::Flow::FailsOn::ClassMethods

Defined in:
lib/axn/core/flow/fails_on.rb

Instance Method Summary collapse

Instance Method Details

#_fails_on?(exception) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/axn/core/flow/fails_on.rb', line 49

def _fails_on?(exception)
  _fails_on_matchers.any? { |klass| exception.is_a?(klass) }
end

#fails_on(exceptions, message = nil, standalone: nil) { ... } ⇒ Object

Parameters:

  • exceptions (Class, Array<Class>)

    one or more Exception classes

  • message (String, #call, nil) (defaults to: nil)

    optional message (positional, like fail!)

  • standalone (Boolean, nil) (defaults to: nil)

    forwarded to the wired error — true lets the message replace a declared base headline instead of attaching under it; only meaningful with a message/block (there is no wired error to configure otherwise)

Yields:

  • optional block receiving the exception (like error { |e| ... })

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/axn/core/flow/fails_on.rb', line 27

def fails_on(exceptions, message = nil, standalone: nil, &block)
  classes = Array(exceptions)
  if classes.empty? || classes.any? { |c| !(c.is_a?(Class) && c <= Exception) }
    raise ArgumentError, "fails_on requires one or more Exception classes (got #{exceptions.inspect})"
  end

  _reject_unreachable_fails_on!(classes)

  # standalone: only configures the wired `error`, so it's inert without a message/block —
  # raise rather than silently drop it (true and false alike), matching the message DSL.
  raise ArgumentError, "fails_on standalone: has no effect without a message or block" if !standalone.nil? && !(message || block)

  self._fails_on_matchers = (_fails_on_matchers + classes).freeze

  # Wire the message through the existing `error` DSL when provided. Uses an OR proc
  # (not `if: classes`) because `if:` with an array matches via `all?` (AND). standalone:
  # is forwarded verbatim (nil = the DSL's conditional default: an attached reason).
  error(message, if: ->(exception:) { classes.any? { |klass| exception.is_a?(klass) } }, standalone:, &block) if message || block

  true
end