Class: Easy::Api::Error

Inherits:
Object
  • Object
show all
Defined in:
lib/easy/api/error.rb

Overview

Encapsulates the types of errors that API calls can respond with

Constant Summary collapse

CODES =
{
  invalid: 400,
  unauthorized: 401,
  not_found: 404,
  unexpected: 500
}
MESSAGES =
{
  invalid: "Invalid request",
  unauthorized: "This request requires a valid Private API Key",
  not_found: "Resource not found",
  unexpected: 'Sorry! An exception has occured. Please try again later',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, msg = nil) ⇒ Error

Initializes a new error based on the type, with an optional custom message

Parameters:

  • type (Symbol)

    can be :invalid, :unauthorized, :not_found, or :unexpected

  • msg (optional, String) (defaults to: nil)

    a custom error message (see MESSAGES for default message values)



24
25
26
27
# File 'lib/easy/api/error.rb', line 24

def initialize(type, msg=nil)
  @code = CODES[type]
  @message = msg || MESSAGES[type]
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



4
5
6
# File 'lib/easy/api/error.rb', line 4

def code
  @code
end

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/easy/api/error.rb', line 4

def message
  @message
end

Instance Method Details

#as_json(options = {}) ⇒ Hash

Used by Rails to parse the error as json

Returns:

  • (Hash)

    a hash containing the error code and message



39
40
41
# File 'lib/easy/api/error.rb', line 39

def as_json(options={})
  to_hash
end

#to_hashHash

Returns the error as a hash

Returns:

  • (Hash)

    a hash containing the error code and message



32
33
34
# File 'lib/easy/api/error.rb', line 32

def to_hash
  {:code => @code, :message => @message}
end