class Telnyx::TelnyxError

TelnyxError is the base error from which all other more specific Telnyx errors derive.

Attributes

errors[R]

Full details for all errors returned in response

http_body[R]
http_headers[R]
http_status[R]
json_body[R]
request_id[R]
response[RW]

Response contains a TelnyxResponse object that has some basic information about the response that conveyed the error.

Public Class Methods

new(errors = nil, http_status: nil, http_body: nil, json_body: nil, http_headers: nil) click to toggle source

Initializes a TelnyxError.

# File lib/telnyx/errors.rb, line 21
def initialize(errors = nil, http_status: nil, http_body: nil, json_body: nil, http_headers: nil)
  @http_status = http_status
  @http_body = http_body
  @http_headers = http_headers || {}
  @json_body = json_body
  @request_id = @http_headers[:request_id]
  @errors = stringify_errors(errors)
end

Public Instance Methods

error_count() click to toggle source
# File lib/telnyx/errors.rb, line 55
def error_count
  case @errors
  when Array
    @errors.count
  else
    1
  end
end
message() click to toggle source
# File lib/telnyx/errors.rb, line 46
def message
  case @errors
  when Array
    "#{@errors[0]['title']} "
  else
    @errors
  end
end
other_errors_message() click to toggle source
# File lib/telnyx/errors.rb, line 37
def other_errors_message
  count = error_count
  if count > 2
    "plus #{count} other errors. "
  elsif count == 2
    "plus 1 other error. "
  end
end
stringify_errors(errors) click to toggle source
# File lib/telnyx/errors.rb, line 72
def stringify_errors(errors)
  if errors.is_a? Array
    errors.map { |h| stringify_hash(h) }
  elsif errors.is_a? Hash
    stringify_hash errors
  else
    errors
  end
end
stringify_hash(h) click to toggle source
# File lib/telnyx/errors.rb, line 64
def stringify_hash(h)
  str_hash = {}
  h.each_key do |k|
    str_hash[k.to_s] = h[k]
  end
  str_hash
end
to_s() click to toggle source
# File lib/telnyx/errors.rb, line 30
def to_s
  status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
  id_string = @request_id.nil? ? "" : "(Request #{@request_id}) "
  instruction = "Full details: #{@errors}"
  "#{status_string}#{id_string}#{message}#{other_errors_message}#{instruction}"
end