Skip to content

Commit de0a0e0

Browse files
authored
Merge pull request #155 from gondalez/master
Add extra context to IntercomError
2 parents 5cef7ad + 3acba1c commit de0a0e0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

lib/intercom/errors.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ module Intercom
22

33
# Base class exception from which all public Intercom exceptions will be derived
44
class IntercomError < StandardError
5-
attr_reader :http_code, :application_error_code
6-
def initialize(message, http_code = nil, error_code = application_error_code)
7-
@http_code = http_code
8-
@application_error_code = error_code
5+
attr_reader :http_code, :application_error_code, :field, :request_id
6+
def initialize(message, context={})
7+
@http_code = context[:http_code]
8+
@application_error_code = context[:application_error_code]
9+
@field = context[:field]
10+
@request_id = context[:request_id]
911
super(message)
1012
end
13+
def inspect
14+
attributes = instance_variables.map do |var|
15+
value = instance_variable_get(var).inspect
16+
"#{var}=#{value}"
17+
end
18+
"##{self.class.name}:#{message} #{attributes.join(' ')}"
19+
end
20+
def to_hash
21+
{message: message}
22+
.merge(Hash[instance_variables.map{ |var| [var[1..-1], instance_variable_get(var)] }])
23+
end
1124
end
1225

1326
# Raised when the credentials you provide don't match a valid account on Intercom.

lib/intercom/request.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,13 @@ def raise_application_errors_on_failure(error_list_details, http_code)
128128
# Currently, we don't support multiple errors
129129
error_details = error_list_details['errors'].first
130130
error_code = error_details['type'] || error_details['code']
131+
error_field = error_details['field']
131132
parsed_http_code = (http_code > 0 ? http_code : nil)
132133
error_context = {
133134
:http_code => parsed_http_code,
134-
:application_error_code => error_code
135+
:application_error_code => error_code,
136+
:field => error_field,
137+
:request_id => error_list_details['request_id']
135138
}
136139
case error_code
137140
when 'unauthorized', 'forbidden'

0 commit comments

Comments
 (0)