Class: Cuprum::Error

Inherits:
Object
  • Object
show all
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.

Examples:

error = Cuprum::Error.new(message: 'Something went wrong')
error.type
#=> 'cuprum.error'
error.message
#=> 'Something went wrong'

An Error With Custom Type

error = Cuprum::Error.new(
  message: 'Something went wrong',
  type:    'custom.errors.generic',
)
error.type
#=> 'custom.errors.generic'

An Error Subclass

class LightsError < Cuprum::Error
  TYPE = 'custom.errors.wrong_number_of_lights'

  def initialize(count)
    super(message: "There are #{count} lights!")

    @count = count
  end

  private def as_json_data
    { 'count' => count }
  end
end

error = LightsError.new(4)
error.type
#=> 'custom.errors.wrong_number_of_lights'
error.message
#=> 'There are 4 lights!'
error.as_json
#=> {
#     'data'    => { 'count' => 4 },
#     'message' => 'There are 4 lights!',
#     'type'    => 'custom.errors.wrong_number_of_lights'
#   }

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

Instance Method Summary collapse

Constructor Details

#initialize(message: nil, type: nil, **properties) ⇒ Error

Returns a new instance of Error.

Parameters:

  • message (String) (defaults to: nil)

    Optional message describing the nature of the error.

  • properties (Hash)

    Additional properties used to compare errors.

  • type (String) (defaults to: nil)

    Short string used to identify the type of error.



65
66
67
68
69
# File 'lib/cuprum/error.rb', line 65

def initialize(message: nil, type: nil, **properties)
  @message               = message
  @type                  = type || self.class::TYPE
  @comparable_properties = properties.merge(message: message, type: type)
end

Instance Attribute Details

#messageString (readonly)

Returns optional message describing the nature of the error.

Returns:

  • (String)

    optional message describing the nature of the error.



72
73
74
# File 'lib/cuprum/error.rb', line 72

def message
  @message
end

#typeString (readonly)

Returns short string used to identify the type of error.

Returns:

  • (String)

    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.

Parameters:

Returns:

  • (Boolean)

    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_jsonHash<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.

Returns:

  • (Hash<String, Object>)

    a serializable hash representation of the error.



95
96
97
98
99
100
101
# File 'lib/cuprum/error.rb', line 95

def as_json
  {
    'data'    => as_json_data,
    'message' => message,
    'type'    => type
  }
end