Exception: Axn::ValidationError

Inherits:
ContractViolation show all
Defined in:
lib/axn/exceptions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors, user_facing: false, user_facing_message: nil) ⇒ ValidationError

user_facing: marks an inbound validation failure that the Executor has reclassified into the failure bucket (see expects ..., user_facing:). The structured errors are preserved on the exception either way; user_facing_message carries the (possibly overridden) message that surfaces on result.error as an attachable reason — headlined by a declared base error just like a fail! reason — leaving the dev-facing #message (full validation errors) intact.



317
318
319
320
321
322
323
# File 'lib/axn/exceptions.rb', line 317

def initialize(errors, user_facing: false, user_facing_message: nil)
  @errors = errors
  @user_facing = user_facing
  @user_facing_message = user_facing_message
  @presentation = nil # set by __present_as when an owned, user-facing failure is stamped (see Axn::Failure)
  super(errors)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



310
311
312
# File 'lib/axn/exceptions.rb', line 310

def errors
  @errors
end

#user_facing_messageObject (readonly)

Returns the value of attribute user_facing_message.



310
311
312
# File 'lib/axn/exceptions.rb', line 310

def user_facing_message
  @user_facing_message
end

Class Method Details

.user_facing?(exception) ⇒ Boolean

Single source of truth for "did this (arbitrary) exception settle into the user-facing failure bucket?" — folds in the is_a? guard so the Executor (classification) and Result (outcome + surfaced reason) ask the question one way and can't drift apart.

Returns:

  • (Boolean)


328
# File 'lib/axn/exceptions.rb', line 328

def self.user_facing?(exception) = exception.is_a?(self) && exception.user_facing?

Instance Method Details

#__present_as(string) ⇒ Object

Normalized on assignment and read back plain, on the same terms as Axn::Failure#__present_as above: what resolution produces can be the caller's own object, and presence dispatched its blank? from inside the path settling the failure.



335
# File 'lib/axn/exceptions.rb', line 335

def __present_as(string) = @presentation = Axn::Internal::NativeMethods.absent_value?(string) ? nil : string

#field_errorsObject

Structured per-field view of the validation errors, for callers that want to format each failure individually (e.g. a tool adapter handing per-argument reasons back to a model). full_message so each entry reads standalone; base-level errors surface with field == :base.



342
# File 'lib/axn/exceptions.rb', line 342

def field_errors = errors.map { |error| { field: error.attribute, message: error.full_message } }

#messageObject



336
# File 'lib/axn/exceptions.rb', line 336

def message = @presentation || errors.full_messages.to_sentence

#to_sObject



337
# File 'lib/axn/exceptions.rb', line 337

def to_s = message

#user_facing?Boolean

Returns:

  • (Boolean)


330
# File 'lib/axn/exceptions.rb', line 330

def user_facing? = @user_facing