Class: Cuprum::Error
- Inherits:
-
Object
- Object
- Cuprum::Error
- Defined in:
- lib/cuprum/error.rb
Overview
Wrapper class for encapsulating an error state for a failed Cuprum result.
Additional details can be passed by setting the #message or by using a subclass of Cuprum::Error.
Direct Known Subclasses
Cuprum::Errors::CommandNotImplemented, Cuprum::Errors::MultipleErrors, Cuprum::Errors::OperationNotCalled, Cuprum::Errors::UncaughtException
Constant Summary collapse
- TYPE =
Short string used to identify the type of error.
Primarily used for serialization. This value can be overriden by passing in the :type parameter to the constructor.
Subclasses of Cuprum::Error should define their own default TYPE constant.
'cuprum.error'
Instance Attribute Summary collapse
-
#message ⇒ String
readonly
Optional message describing the nature of the error.
-
#type ⇒ String
readonly
Short string used to identify the type of error.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
True if the other object has the same class and properties; otherwise false.
-
#as_json ⇒ Hash<String, Object>
Generates a serializable representation of the error object.
-
#initialize(message: nil, type: nil, **properties) ⇒ Error
constructor
A new instance of Error.
Constructor Details
#initialize(message: nil, type: nil, **properties) ⇒ Error
Returns a new instance of Error.
65 66 67 68 69 |
# File 'lib/cuprum/error.rb', line 65 def initialize(message: nil, type: nil, **properties) @message = @type = type || self.class::TYPE @comparable_properties = properties.merge(message: , type: type) end |
Instance Attribute Details
#message ⇒ String (readonly)
Returns optional message describing the nature of the error.
72 73 74 |
# File 'lib/cuprum/error.rb', line 72 def @message end |
#type ⇒ String (readonly)
Returns short string used to identify the type of error.
75 76 77 |
# File 'lib/cuprum/error.rb', line 75 def type @type end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if the other object has the same class and properties; otherwise false.
81 82 83 84 |
# File 'lib/cuprum/error.rb', line 81 def ==(other) other.instance_of?(self.class) && other.comparable_properties == comparable_properties end |
#as_json ⇒ Hash<String, Object>
Generates a serializable representation of the error object.
By default, contains the #type and #message properties and an empty :data Hash. This can be overriden in subclasses by overriding the private method #as_json_data; this should always return a Hash with String keys and whose values are basic objects or data structures of the same.
95 96 97 98 99 100 101 |
# File 'lib/cuprum/error.rb', line 95 def as_json { 'data' => as_json_data, 'message' => , 'type' => type } end |