Class: OmniService::Error

Inherits:
Dry::Struct
  • Object
show all
Defined in:
lib/omni_service/error.rb

Overview

Structured validation/operation error.

Attributes:

  • component: the component that produced the error

  • code: symbolic error code (e.g., :blank, :not_found, :invalid)

  • message: human-readable message (optional)

  • path: array of keys/indices to error location (e.g., [:author, :email])

  • tokens: interpolation values for message templates (e.g., { min: 5 })

Components return failures that are converted to Error:

  • Failure(:code) => Error(code: :code)

  • Failure(“message”) => Error(message: “message”)

  • Failure({ code:, path:, message:, tokens: }) => Error(…)

  • Failure() => multiple Errors

Examples:

Simple error

Failure(:not_found)
# => Error(code: :not_found, path: [])

Error with path

Failure([{ code: :blank, path: [:title] }])
# => Error(code: :blank, path: [:title])

Error with tokens for i18n

Failure([{ code: :too_short, path: [:body], tokens: { min: 100 } }])

Class Method Summary collapse

Class Method Details

.build(component) ⇒ Object



53
54
55
# File 'lib/omni_service/error.rb', line 53

def self.build(component, **)
  new(message: nil, code: nil, path: [], tokens: {}, **, component:)
end

.process(component, failure) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omni_service/error.rb', line 36

def self.process(component, failure)
  case failure
  in Array
    failure.flat_map { |error| process(component, error) }
  in OmniService::Error
    failure.new(component:)
  in String
    [build(component, message: failure)]
  in Symbol
    [build(component, code: failure)]
  in Hash
    [build(component, **failure)]
  else
    raise "Invalid failure `#{failure.inspect}` returned by `#{component.inspect}`"
  end
end