Exception: Controls::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/controls/objects/error.rb

Overview

A class under the Controls namespace to wrap API errors

review
  • subclass Dish::Plate instead of StandardError?

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ self

Returns the Controls::Error with the given attributes.

Parameters:

  • attributes (Hash) (defaults to: {})

    the key/value pairs to set instance variables with

  • :message (Hash)

    a customizable set of options

  • :status (Hash)

    a customizable set of options



17
18
19
20
21
22
# File 'lib/controls/objects/error.rb', line 17

def initialize(attributes = {})
  @__attributes__ = attributes
  @__attributes__.each do |attribute, value|
    instance_variable_set(:"@#{attribute}", value)
  end
end

Instance Attribute Details

#message=(value) ⇒ String

Returns the message related to the error.

Returns:

  • (String)

    the message related to the error



10
11
12
# File 'lib/controls/objects/error.rb', line 10

def message
  @message
end

#statusString

Returns the status code for the error response.

Returns:

  • (String)

    the status code for the error response



10
# File 'lib/controls/objects/error.rb', line 10

attr_accessor :message, :status

Instance Method Details

#inspectString

Returns a string representing the error and all of it’s attributes.

Returns:

  • (String)

    a string representing the error and all of it’s attributes



26
27
28
29
30
31
32
# File 'lib/controls/objects/error.rb', line 26

def inspect
  vars = to_h.map do |attribute, value|
    "#{attribute}: #{value}"
  end

  "#<#{self.class}: #{vars.join(', ')}>"
end

#to_hHash

Returns the attributes used to initialize this error.

Returns:

  • (Hash)

    the attributes used to initialize this error



40
41
42
# File 'lib/controls/objects/error.rb', line 40

def to_h
  @__attributes__
end

#to_jsonString

Returns the JSON representation of the attributes.

Returns:

  • (String)

    the JSON representation of the attributes



35
36
37
# File 'lib/controls/objects/error.rb', line 35

def to_json
  @__attributes__.to_json
end

#to_sString

Returns the error message if available otherwise calls #inspect.

Returns:

  • (String)

    the error message if available otherwise calls #inspect



45
46
47
# File 'lib/controls/objects/error.rb', line 45

def to_s
  @message or inspect
end