Module: Castkit::AttributeExtensions::ErrorHandling

Included in:
Casting, Validation
Defined in:
lib/castkit/attribute_extensions/error_handling.rb

Overview

Provides centralized handling of attribute casting and validation errors.

The behavior of each error is controlled by configuration flags in ‘Castkit.configuration`.

Constant Summary collapse

ERROR_OPTIONS =

Maps known error types to their handling behavior.

Each entry includes:

  • ‘:config` – the config flag that determines enforcement

  • ‘:message` – a lambda that generates an error message

  • ‘:error` – the error class to raise

Returns:

  • (Hash<Symbol, Hash>)
{
  array_of_type: {
    config: :enforce_array_of_type,
    message: ->(*_) { "`of:` must be provided for array type" },
    error: Castkit::AttributeError
  },
  primitive: {
    config: :enforce_known_primitive_type,
    message: ->(_attr, type:) { "unknown primitive type: #{type.inspect}" },
    error: Castkit::AttributeError
  },
  boolean: {
    config: :enforce_boolean_casting,
    message: ->(_attr, value:) { "must be a boolean, got: #{value.inspect}" },
    error: Castkit::AttributeError
  },
  union: {
    config: :enforce_union_match,
    message: ->(_attr, types:) { "could not be cast to any of #{types.inspect}" },
    error: Castkit::AttributeError
  },
  access: {
    config: :enforce_attribute_access,
    message: ->(_attr, mode:) { "invalid access mode `#{mode}`" },
    error: Castkit::AttributeError
  },
  unwrapped: {
    config: :enforce_unwrapped_prefix,
    message: ->(*_) { "prefix can only be used with unwrapped attribute" },
    error: Castkit::AttributeError
  },
  array_options: {
    config: :enforce_array_options,
    message: ->(*_) { "array attribute must specify `of:` type" },
    error: Castkit::AttributeError
  }
}.freeze