Module: Weft::DSL::Recoveries::ClassMethods

Defined in:
lib/weft/dsl/recoveries.rb

Instance Method Summary collapse

Instance Method Details

#component_recovery_for(exception) ⇒ Object

Like recovery_for, but skips entries whose target resolves to a Page — for contexts that can only render component fragments (an SSE push can't redirect or full-page swap). The gem-default StandardError entry at the bottom of every component chain resolves to a component, so StandardError-family exceptions always find a match.



66
67
68
69
70
71
# File 'lib/weft/dsl/recoveries.rb', line 66

def component_recovery_for(exception)
  recoveries.find do |entry|
    recovery_matches?(entry[:from], exception) &&
      !page_recovery_target?(resolve_recovery_target(entry))
  end
end

#recoveriesObject

All declared recovery entries (own + inherited), in resolution order. Own entries precede inherited entries so subclass declarations take precedence over ancestor declarations. Within a class, declaration order is preserved — first-declared wins on ties.



45
46
47
48
49
50
51
# File 'lib/weft/dsl/recoveries.rb', line 45

def recoveries
  if superclass.respond_to?(:recoveries)
    own_recoveries + superclass.recoveries
  else
    own_recoveries.dup
  end
end

#recovers(from:, with: nil, status: nil, &block) ⇒ Object

Declare a recovery edge: how this class handles a specific error.

recovers from: Weft::Unprocessable do |params, error|
{ error_messages: error.messages }
end

recovers from: Weft::Unauthorized, with: LoginPage
recovers from: ActiveRecord::RecordNotFound, with: NotFoundPage, status: 404

from: accepts a Class (subclass-inclusive), Integer (matched against HTTPError#status), Range, or Array of any of the above. with: accepts a Class (Page or Component) or Symbol (resolved against Weft.configuration at error-handling time). Default: self. status: declares what the error means on the wire — recoveries from errors that aren't Weft::HTTPErrors report 500 without it. Must be an HTTP error status (400..599); raises Weft::InvalidUsage otherwise. The optional block receives |params, error| and returns a hash of additional params that merge with the original on the recovery edge. Symmetric with performs/transfers contracts (params first; error is the recovery-specific extra). The block never returns HTML.



36
37
38
39
# File 'lib/weft/dsl/recoveries.rb', line 36

def recovers(from:, with: nil, status: nil, &block)
  validate_recovery_status!(status)
  own_recoveries << { from: from, with: with, status: status, block: block }
end

#recovery_for(exception) ⇒ Object

Find the first recovery entry whose from: matches the given exception. Returns nil if nothing matches. from: accepts Class (subclass-inclusive), Integer (status equality — HTTPError carries .status; non-HTTPError = 500), Range (status in range), or Array of any of the above (any element matches).



57
58
59
# File 'lib/weft/dsl/recoveries.rb', line 57

def recovery_for(exception)
  recoveries.find { |entry| recovery_matches?(entry[:from], exception) }
end

#resolve_recovery_target(entry) ⇒ Object

Resolve the recovery entry's with: value to a concrete target class. Symbol values look up Weft.configuration.<sym> (resolved at error-handling time so config reassignment propagates). Nil falls back to self.



76
77
78
79
80
81
82
# File 'lib/weft/dsl/recoveries.rb', line 76

def resolve_recovery_target(entry)
  case entry[:with]
  when Symbol then Weft.configuration.public_send(entry[:with])
  when nil    then self
  else entry[:with]
  end
end