Exception: NRSER::Types::CheckError

Inherits:
TypeError
  • Object
show all
Includes:
NicerError
Defined in:
lib/nrser/types/errors/check_error.rb

Overview

This error (or a subclass) is thrown when types fail to Type.check!.

Constant Summary

Constants included from NicerError

NicerError::DEFAULT_COLUMN_WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NicerError

#add_extended_message?, column_width, #context, #context_section, #details, #details_section, #extended_message, #format_message, #format_message_segment, #to_s

Constructor Details

#initialize(*message, value:, type:, details: nil, **kwds) ⇒ CheckError

Construct a ‘NicerError`.

Parameters:

  • value: (*)

    The #value that failed the check.

  • type: (NRSER::Types::Type)

    The type that was checked.

  • **kwds
  • details: (nil | String | Proc<()=>String> | #to_s) (defaults to: nil)

    Additional text details to add to the extended message. When:

    1. ‘nil` - no details will be added.

    2. ‘String` - the value will be used. If `binding:` is provided, it will be rendered against it as ERB.

    3. ‘Proc<()=>String>` - if and when an extended message is needed the proc will be called, and the resulting string will be used as in (2).

    4. ‘#to_s` - catch all; if and when an extended message is needed `#to_s` will be called on the value and the result will be used as in (2).



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/nrser/types/errors/check_error.rb', line 59

def initialize *message, value:, type:, details: nil, **kwds
  @value = value
  @type = type
  
  if details.is_a?( Proc ) && details.arity != 0
    orig_details = details
    details = -> { orig_details.call type: type, value: value }
  end
  
  super \
    *message,
    type: type,
    value: value,
    details: details,
    **kwds
end

Instance Attribute Details

#typeNRSER::Types::Type (readonly)

The type that was checked against.

Returns:



33
34
35
# File 'lib/nrser/types/errors/check_error.rb', line 33

def type
  @type
end

#value* (readonly)

The value that failed the type check.

Returns:

  • (*)


40
41
42
# File 'lib/nrser/types/errors/check_error.rb', line 40

def value
  @value
end

Instance Method Details

#default_messageString

Build default message when none provided.

Returns:



81
82
83
# File 'lib/nrser/types/errors/check_error.rb', line 81

def default_message
  ["Value", value.inspect, "failed check for type", type.name]
end