Skip to content

Commit b694545

Browse files
committed
Merge pull request #124 from intercom/jo/throw-error-on-nil-response
add logic to throw error when unexpected nil received
2 parents 5c0aef5 + 7f7ddc0 commit b694545

File tree

6 files changed

+30
-0
lines changed

6 files changed

+30
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ Intercom::BadRequestError
343343
Intercom::RateLimitExceeded
344344
Intercom::AttributeNotSetError # Raised when you try to call a getter that does not exist on an object
345345
Intercom::MultipleMatchingUsersError
346+
Intercom::HttpError # Raised when response object is unexpectedly nil
346347
```
347348

348349
### Rate Limiting

lib/intercom/api_operations/find.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def find(params)
1010
else
1111
response = Intercom.get("/#{collection_name}", params)
1212
end
13+
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
1314
from_api(response)
1415
end
1516
end

lib/intercom/api_operations/load.rb

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ def load
88
else
99
raise "Cannot load #{self.class} as it does not have a valid id."
1010
end
11+
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response
1112
from_response(response)
1213
end
1314
end

lib/intercom/collection_proxy.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def each(&block)
2121
else
2222
response_hash = Intercom.get(@finder_url, @finder_params)
2323
end
24+
raise Intercom::HttpError.new('Http Error - No response entity returned') unless response_hash
2425
deserialize_response_hash(response_hash, block)
2526
next_page = extract_next_link(response_hash)
2627
break if next_page.nil?

lib/intercom/errors.rb

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class MultipleMatchingUsersError < IntercomError; end
4343

4444
# Raised when you try to call a non-setter method that does not exist on an object
4545
class Intercom::AttributeNotSetError < IntercomError ; end
46+
47+
# Raised when unexpected nil returned from server
48+
class Intercom::HttpError < IntercomError ; end
4649

4750
#
4851
# Non-public errors (internal to the gem)

spec/unit/intercom/company_spec.rb

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require 'spec_helper'
2+
3+
describe Intercom::Company do
4+
5+
describe 'when no response raises error' do
6+
it 'on find' do
7+
Intercom.expects(:get).with("/companies", {:company_id => '4'}).returns(nil)
8+
proc {company = Intercom::Company.find(:company_id => '4')}.must_raise Intercom::HttpError
9+
end
10+
11+
it 'on find_all' do
12+
Intercom.expects(:get).with("/companies", {}).returns(nil)
13+
proc {Intercom::Company.all.each {|company| }}.must_raise Intercom::HttpError
14+
end
15+
16+
it 'on load' do
17+
Intercom.expects(:get).with("/companies", {:company_id => '4'}).returns({'type' =>'user', 'id' =>'aaaaaaaaaaaaaaaaaaaaaaaa', 'company_id' => '4', 'name' => 'MyCo'})
18+
company = Intercom::Company.find(:company_id => '4')
19+
Intercom.expects(:get).with('/companies/aaaaaaaaaaaaaaaaaaaaaaaa', {}).returns(nil)
20+
proc {company.load}.must_raise Intercom::HttpError
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)