Skip to content

Commit c275eca

Browse files
authored
Merge pull request #372 from intercom/sofia/resource_not_unique_error
added new error for specific error code "resource_conflict"
2 parents 3aa9be0 + 161e1c0 commit c275eca

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This version of the gem is compatible with `Ruby 2.1` and above.
2222

2323
Using bundler:
2424

25-
gem 'intercom', '~> 3.5.22'
25+
gem 'intercom', '~> 3.5.23'
2626

2727
## Basic Usage
2828

changes.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
3.5.23
2+
- New type of error (ResourceNotUniqueError). Thrown when trying to create a resource that already exists in Intercom
3+
14
3.5.22
25
- Return object type
36

lib/intercom/errors.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class ServiceConnectionError < IntercomError; end
4141

4242
# Raised when requesting resources on behalf of a user that doesn't exist in your application on Intercom.
4343
class ResourceNotFound < IntercomError; end
44-
44+
45+
# Raised when trying to create a resource that already exists in Intercom.
46+
class ResourceNotUniqueError < IntercomError; end
47+
4548
# Raised when the request has bad syntax
4649
class BadRequestError < IntercomError; end
4750

lib/intercom/request.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ def raise_application_errors_on_failure(error_list_details, http_code)
163163
raise Intercom::ServiceUnavailableError.new(error_details['message'], error_context)
164164
when 'conflict', 'unique_user_constraint'
165165
raise Intercom::MultipleMatchingUsersError.new(error_details['message'], error_context)
166+
when 'resource_conflict'
167+
raise Intercom::ResourceNotUniqueError.new(error_details['message'], error_context)
166168
when nil, ''
167169
raise Intercom::UnexpectedError.new(message_for_unexpected_error_without_type(error_details, parsed_http_code), error_context)
168170
else

spec/unit/intercom/request_spec.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
it 'raises an error when a html error page rendered' do
88
response = OpenStruct.new(:code => 500)
99
req = Intercom::Request.new('path/', 'GET')
10-
proc {req.parse_body('<html>somethjing</html>', response)}.must_raise(Intercom::ServerError)
10+
proc {req.parse_body('<html>something</html>', response)}.must_raise(Intercom::ServerError)
1111
end
1212

1313
it 'raises a RateLimitExceeded error when the response code is 429' do
1414
response = OpenStruct.new(:code => 429)
1515
req = Intercom::Request.new('path/', 'GET')
16-
proc {req.parse_body('<html>somethjing</html>', response)}.must_raise(Intercom::RateLimitExceeded)
16+
proc {req.parse_body('<html>something</html>', response)}.must_raise(Intercom::RateLimitExceeded)
1717
end
1818

1919
it 'parse_body raises an error if the decoded_body is "null"' do
@@ -23,8 +23,8 @@
2323
end
2424

2525
describe 'Intercom::Client' do
26-
let (:client) { Intercom::Client.new(token: 'foo', handle_rate_limit: true) }
27-
let (:uri) {"https://api.intercom.io/users"}
26+
let(:client) { Intercom::Client.new(token: 'foo', handle_rate_limit: true) }
27+
let(:uri) {"https://api.intercom.io/users"}
2828

2929
it 'should have handle_rate_limit set' do
3030
client.handle_rate_limit.must_equal(true)
@@ -63,6 +63,18 @@
6363

6464
end
6565

66+
67+
describe "Application errors on failure" do
68+
let(:uri) {"https://api.intercom.io/conversations/reply"}
69+
it 'should raise ResourceNotUniqueError error on resource_conflict code' do
70+
# Use webmock to mock the HTTP request
71+
stub_request(:put, uri).\
72+
to_return(status: [409, "Resource Already Exists"], headers: { 'X-RateLimit-Reset' => Time.now.utc + 10 }, body: {type: "error.list", errors: [ code: "resource_conflict" ]}.to_json)
73+
req = Intercom::Request.put(uri, "")
74+
expect { req.execute(target_base_url=uri, username: "ted", secret: "") }.must_raise(Intercom::ResourceNotUniqueError)
75+
end
76+
end
77+
6678
it 'parse_body returns nil if decoded_body is nil' do
6779
response = OpenStruct.new(:code => 500)
6880
req = Intercom::Request.new('path/', 'GET')

0 commit comments

Comments
 (0)