Exception: NRSER::ValueError
- Defined in:
- lib/nrser/errors/value_error.rb
Overview
Raised when there is a problem with a value that does not fall into one of the other built-in exception categories (non-exhaustive list):
-
It’s the wrong type (TypeError)
-
It’s an argument (ArgumentError)
The invalid value is attached to the error as an instance value so that rescuers up the stack can do more intelligent things with it if need be.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#subject ⇒ Object
readonly
The invalid value.
Instance Method Summary collapse
-
#build_message ⇒ String
Build the error message when none is provided to ‘#initialize`.
-
#initialize(message = nil, subject:) ⇒ ValueError
constructor
A new instance of ValueError.
Constructor Details
#initialize(message = nil, subject:) ⇒ ValueError
Returns a new instance of ValueError.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/nrser/errors/value_error.rb', line 22 def initialize = nil, subject: @subject = subject # If we received `nil` for the message, call {#build_message} to get it. # # This provides a "hook" to assemble the message at the last possible # moment before it needs to go up to {StandardError#initialize}, allowing # {#build_message} to work with an otherwise fully-initialized instance. # # Of course, {NRSER::ValueError#build_message} # throws {NotImplementedError} since it doesn't really have enough # knowledge to build anything useful (we're going for useful errors, # "Value #{ value } is invalid" does not suffice). # = if .nil? super end |
Instance Attribute Details
#subject ⇒ Object (readonly)
The invalid value.
19 20 21 |
# File 'lib/nrser/errors/value_error.rb', line 19 def subject @subject end |
Instance Method Details
#build_message ⇒ String
Build the error message when none is provided to ‘#initialize`.
When no ‘message` (or `nil`) is provided to initialize it will call this method to get the error message just before it needs it to call up to StandardError#initialize (via `super message`).
This allows NRSER::ValueError subclasses that are able to build a useful default message or would like to augment the user-provided one to do so at the last possible moment before it’s needed, letting them work with an otherwise fully-initialized instance.
Hence a subclass several generations down from NRSER::ValueError can use values initialized in all the constructors in-between, avoiding a lot of headache.
This implementation always raises AbstractMethodError because NRSER::ValueError does not have enough information to construct a useful message.
68 69 70 |
# File 'lib/nrser/errors/value_error.rb', line 68 def raise NRSER::AbstractMethodError.new( self, __method__ ) end |