Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 61d24ba

Browse files
author
Andrej Jančič
committed
find_by_id and RecordNotFound
1 parent 9a9c126 commit 61d24ba

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

lib/creatubbles.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def self.instantiate_objects_from_response(response, connection)
4242

4343
end
4444

45+
require "creatubbles/error"
4546
require "creatubbles/utils/auth_details"
4647
require "creatubbles/content"
4748
require "creatubbles/contents"

lib/creatubbles/base_collection.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,23 @@ def initialize(connection)
1313
@connection = connection
1414
end
1515

16-
def find(id,opts={nil_404:false})
16+
def find(id)
1717
res = @connection.get("#{self.class.type_name}/#{id}")
1818
Creatubbles.instantiate_object_from_response(res, @connection)
1919
rescue => e
20-
if opts[:nil_404] && is_record_not_found_404?(e)
21-
return nil
20+
if is_record_not_found_404?(e)
21+
raise Creatubbles::Error::RecordNotFound.new(e), "Not Found! #{self.class.type_name}/#{id}"
2222
else
2323
raise e
2424
end
2525
end
2626

27+
def find_by_id(id)
28+
find(id)
29+
rescue Creatubbles::Error::RecordNotFound
30+
return nil
31+
end
32+
2733
def init_objects(response)
2834
Creatubbles.instantiate_objects_from_response(response, @connection)
2935
end

lib/creatubbles/error.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Creatubbles
2+
module Error
3+
#Creatubbles::Error::RecordNotFound
4+
class RecordNotFound < StandardError
5+
attr_reader :original
6+
def initialize original
7+
@original = original
8+
end
9+
end
10+
end
11+
end

spec/creatubbles/base_collection_spec.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
client = Creatubbles::Client.dummy
77

88
expect{client.test_objects.find('non-existing-object-id')}.
9-
to raise_error(OAuth2::Error)
9+
to raise_error(Creatubbles::Error::RecordNotFound)
1010

11-
expect{client.test_objects.find('non-existing-object-id', nil_404:true)}.
12-
not_to raise_error
11+
begin
12+
client.test_objects.find('non-existing-object-id')
13+
rescue => e
14+
expect(e.original).to be_a(OAuth2::Error)
15+
expect(e.message).to include('non-existing-object-id')
16+
end
1317

14-
expect(client.test_objects.find('non-existing-object-id', nil_404:true)).
18+
expect(client.test_objects.find_by_id('non-existing-object-id')).
1519
to be_nil
1620
end
1721
end

0 commit comments

Comments
 (0)