Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit 0f1359c

Browse files
committed
Add custom exception for error responses
1 parent 1906cfd commit 0f1359c

File tree

7 files changed

+45
-26
lines changed

7 files changed

+45
-26
lines changed

lib/thingiverse.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
require 'thingiverse/dynamic_attributes'
99
require 'thingiverse/connection'
10+
require 'thingiverse/response_error'
1011
require 'thingiverse/pagination'
1112
require 'thingiverse/things'
1213
require 'thingiverse/files'

lib/thingiverse/connection.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def access_token=(token)
3535
def get_token
3636
auth_response = self.class.post(auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code})
3737

38-
raise "#{auth_response.code}: #{auth_response.body.inspect}" unless auth_response.success?
38+
raise ResponseError.new(auth_response) unless auth_response.success?
3939

4040
response = CGI::parse(auth_response.parsed_response)
4141

@@ -53,7 +53,7 @@ def things
5353
def users
5454
Thingiverse::Users
5555
end
56-
56+
5757
def tags
5858
Thingiverse::Tags
5959
end

lib/thingiverse/pagination.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ module Thingiverse
44
class Pagination
55
include Enumerable
66
extend Forwardable
7-
7+
88
attr_reader :response, :object
99
attr_reader :current_page, :total_pages, :first_url, :last_url, :next_url, :prev_url
10-
10+
1111
def initialize(response, object)
1212
@response = response
1313
@object = object
1414

15-
# TODO: provide more debug info and raise a custom exception
16-
raise "#{@response.code}: #{JSON.parse(@response.body)['error']}" unless @response.success?
17-
15+
raise ResponseError.new(@response) unless @response.success?
16+
1817
@objects = @response.parsed_response.collect do |attrs|
1918
@object.new attrs
2019
end
@@ -38,22 +37,22 @@ def initialize(response, object)
3837
end
3938

4039
def_delegators :@objects, :<<, :[], :[]=, :last, :size
41-
40+
4241
def method_missing(meth, *args, &block)
4342
if meth.to_s =~ /^(.*)_page$/
4443
get_url_page($1, *args, &block)
4544
else
4645
super
4746
end
4847
end
49-
48+
5049
def get_url_page(which, *args, &block)
5150
url = instance_variable_get("@#{which}_url")
5251
Thingiverse::Pagination.new(Thingiverse::Connection.get(url), @object) if url
5352
end
54-
53+
5554
def each(&block)
5655
@objects.each(&block)
57-
end
56+
end
5857
end
5958
end

lib/thingiverse/response_error.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Thingiverse
2+
class ResponseError < StandardError
3+
def initialize(response)
4+
@response = response
5+
end
6+
7+
attr_reader :response
8+
9+
def message
10+
"#{response.code}: #{message_body} #{response.headers['x-error']}".strip
11+
end
12+
13+
def message_body
14+
JSON.parse(response.body)['error']
15+
rescue JSON::ParserError
16+
response.body
17+
end
18+
end
19+
end

lib/thingiverse/tags.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ class Tags
44

55
def self.find(tag_name)
66
response = Thingiverse::Connection.get("/tags/#{tag_name}")
7-
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
7+
raise ResponseError.new(response) unless response.success?
88
self.new response.parsed_response
99
end
10-
10+
1111
def things(query = {})
1212
Thingiverse::Pagination.new(Thingiverse::Connection.get(things_url, :query => query), Thingiverse::Things)
1313
end

lib/thingiverse/things.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Things
44

55
def user
66
response = Thingiverse::Connection.get("/users/#{creator['name']}")
7-
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
7+
raise ResponseError.new(response) unless response.success?
88
Thingiverse::Users.new response.parsed_response
99
end
1010

@@ -26,7 +26,7 @@ def ancestors(query = {})
2626

2727
def tags
2828
response = Thingiverse::Connection.get(tags_url)
29-
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
29+
raise ResponseError.new(response) unless response.success?
3030
response.parsed_response.collect do |attrs|
3131
Thingiverse::Tags.new attrs
3232
end
@@ -37,7 +37,7 @@ def save
3737
thing = Thingiverse::Things.create(@attributes)
3838
else
3939
response = Thingiverse::Connection.patch("/things/#{id}", :body => @attributes.to_json)
40-
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
40+
raise ResponseError.new(response) unless response.success?
4141

4242
thing = Thingiverse::Things.new(response.parsed_response)
4343
end
@@ -46,7 +46,7 @@ def save
4646
send("#{name}=", value)
4747
end
4848
end
49-
49+
5050
# file_or_string can be a File or a String.
5151
# thingiverse_filename is optional if using a File (the File filename will be used by default) but is required if using a String
5252
def upload(file_or_string, thingiverse_filename=nil)
@@ -59,11 +59,11 @@ def upload(file_or_string, thingiverse_filename=nil)
5959
else
6060
raise ArgumentError, "file_or_string not of accepted type. Expected File or String. Actual: #{file_or_string.class}"
6161
end
62-
62+
6363
raise ArgumentError, "Unable to determine filename" if thingiverse_filename.to_s == ""
64-
64+
6565
response = Thingiverse::Connection.post("/things/#{id}/files", :body => {:filename => thingiverse_filename}.to_json)
66-
raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
66+
raise ResponseError.new(response) unless response.success?
6767

6868
parsed_response = JSON.parse(response.body)
6969
action = parsed_response["action"]
@@ -96,7 +96,7 @@ def upload(file_or_string, thingiverse_filename=nil)
9696
if c.response_code == 303
9797
# finalize it
9898
response = Thingiverse::Connection.post(query['success_action_redirect'])
99-
raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
99+
raise ResponseError.new(response) unless response.success?
100100
Thingiverse::Files.new(response.parsed_response)
101101
else
102102
raise "#{c.response_code}: #{c.body_str}"
@@ -108,7 +108,7 @@ def publish
108108
raise "Cannot publish until thing is saved"
109109
else
110110
response = Thingiverse::Connection.post("/things/#{id}/publish")
111-
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
111+
raise ResponseError.new(response) unless response.success?
112112

113113
thing = Thingiverse::Things.new(response.parsed_response)
114114
end
@@ -120,7 +120,7 @@ def publish
120120

121121
def self.find(thing_id)
122122
response = Thingiverse::Connection.get("/things/#{thing_id}")
123-
raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
123+
raise ResponseError.new(response) unless response.success?
124124
self.new response.parsed_response
125125
end
126126

@@ -132,7 +132,7 @@ def self.create(params)
132132
thing = self.new(params)
133133

134134
response = Thingiverse::Connection.post('/things', :body => thing.attributes.to_json)
135-
raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
135+
raise ResponseError.new(response) unless response.success?
136136

137137
self.new(response.parsed_response)
138138
end

lib/thingiverse/users.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module Thingiverse
22
class Users
33
include Thingiverse::DynamicAttributes
4-
4+
55
def self.find(user_name)
66
response = Thingiverse::Connection.get("/users/#{user_name}")
7-
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
7+
raise ResponseError.new(response) unless response.success?
88
self.new response.parsed_response
99
end
1010
end

0 commit comments

Comments
 (0)