class DeskApi::Error

{DeskApi::Error} is the base error for all {DeskApi} errors.

@author Thomas Stachl <tstachl@salesforce.com> @copyright Copyright © 2013-2016 Salesforce.com @license BSD 3-Clause License

Attributes

rate_limit[R]
response[R]

Public Class Methods

new(err = $ERROR_INFO, response = {}) click to toggle source

Initializes a new Error object

@param err [Exception, String] @param response [Hash] @return [DeskApi::Error]

Calls superclass method
# File lib/desk_api/error.rb, line 46
def initialize(err = $ERROR_INFO, response = {})
  @response    = response
  @rate_limit  = ::DeskApi::RateLimit.new(@response[:response_headers])
  @wrapped_err = err
  @code        = @response[:status]
  @errors      = error_hash(@response[:body])
  err.respond_to?(:backtrace) ? super(err.message) : super(err.to_s)
end

Private Class Methods

descendants() click to toggle source

@return [Array]

# File lib/desk_api/error.rb, line 88
def descendants
  @descendants ||= []
end
error_message(body = nil) click to toggle source

@return [String/Nil]

# File lib/desk_api/error.rb, line 100
def error_message(body = nil)
  if body && body.is_a?(Hash)
    body.key?('message') ? body['message'] : nil
  end
end
errors() click to toggle source

@return [Hash]

# File lib/desk_api/error.rb, line 81
def errors
  @errors ||= descendants.each_with_object({}) do |klass, hash|
    hash[klass::HTTP_STATUS_CODE] = klass if defined? klass::HTTP_STATUS_CODE
  end
end
from_response(response) click to toggle source

Create a new error from an HTTP response

@param response [Hash] @return [DeskApi::Error]

# File lib/desk_api/error.rb, line 76
def from_response(response)
  new(error_message(response[:body]), response)
end
inherited(descendant) click to toggle source

@return [Array]

# File lib/desk_api/error.rb, line 93
def inherited(descendant)
  descendants << descendant
end

Public Instance Methods

backtrace() click to toggle source

Returns the backtrace of the wrapped exception if exits.

@return [String]

Calls superclass method
# File lib/desk_api/error.rb, line 58
def backtrace
  @wrapped_err.respond_to?(:backtrace) ? @wrapped_err.backtrace : super
end

Private Instance Methods

error_hash(body = nil) click to toggle source

@return [Hash/Nil]

# File lib/desk_api/error.rb, line 65
def error_hash(body = nil)
  if body && body.is_a?(Hash)
    body.key?('errors') ? body['errors'] : nil
  end
end