Exception: Telnyx::TelnyxError

Inherits:
StandardError
  • Object
show all
Defined in:
lib/telnyx/errors.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = nil, http_status: nil, http_body: nil, json_body: nil, http_headers: nil) ⇒ TelnyxError

Initializes a TelnyxError.



21
22
23
24
25
26
27
28
# 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

Instance Attribute Details

#errorsObject (readonly)

Full details for all errors returned in response



8
9
10
# File 'lib/telnyx/errors.rb', line 8

def errors
  @errors
end

#http_bodyObject (readonly)

Returns the value of attribute http_body.



14
15
16
# File 'lib/telnyx/errors.rb', line 14

def http_body
  @http_body
end

#http_headersObject (readonly)

Returns the value of attribute http_headers.



15
16
17
# File 'lib/telnyx/errors.rb', line 15

def http_headers
  @http_headers
end

#http_statusObject (readonly)

Returns the value of attribute http_status.



16
17
18
# File 'lib/telnyx/errors.rb', line 16

def http_status
  @http_status
end

#json_bodyObject (readonly)

equivalent to #data



17
18
19
# File 'lib/telnyx/errors.rb', line 17

def json_body
  @json_body
end

#request_idObject (readonly)

Returns the value of attribute request_id.



18
19
20
# File 'lib/telnyx/errors.rb', line 18

def request_id
  @request_id
end

#responseObject

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



12
13
14
# File 'lib/telnyx/errors.rb', line 12

def response
  @response
end

Instance Method Details

#error_countObject



55
56
57
58
59
60
61
62
# File 'lib/telnyx/errors.rb', line 55

def error_count
  case @errors
  when Array
    @errors.count
  else
    1
  end
end

#messageObject



46
47
48
49
50
51
52
53
# File 'lib/telnyx/errors.rb', line 46

def message
  case @errors
  when Array
    "#{@errors[0]['title']} "
  else
    @errors
  end
end

#other_errors_messageObject



37
38
39
40
41
42
43
44
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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_sObject



30
31
32
33
34
35
# 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