Skip to content

Commit

Permalink
Add custom exception for error responses
Browse files Browse the repository at this point in the history
  • Loading branch information
sunny committed Jan 3, 2023
1 parent 1906cfd commit b0d02de
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/thingiverse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require 'thingiverse/dynamic_attributes'
require 'thingiverse/connection'
require 'thingiverse/response_error'
require 'thingiverse/pagination'
require 'thingiverse/things'
require 'thingiverse/files'
Expand Down
4 changes: 2 additions & 2 deletions lib/thingiverse/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def access_token=(token)
def get_token
auth_response = self.class.post(auth_url, :query => {:client_id => @client_id, :client_secret => @client_secret, :code => @code})

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

response = CGI::parse(auth_response.parsed_response)

Expand All @@ -53,7 +53,7 @@ def things
def users
Thingiverse::Users
end

def tags
Thingiverse::Tags
end
Expand Down
17 changes: 8 additions & 9 deletions lib/thingiverse/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@ module Thingiverse
class Pagination
include Enumerable
extend Forwardable

attr_reader :response, :object
attr_reader :current_page, :total_pages, :first_url, :last_url, :next_url, :prev_url

def initialize(response, object)
@response = response
@object = object

# TODO: provide more debug info and raise a custom exception
raise "#{@response.code}: #{JSON.parse(@response.body)['error']}" unless @response.success?

raise ResponseError.new(@response) unless @response.success?

@objects = @response.parsed_response.collect do |attrs|
@object.new attrs
end
Expand All @@ -38,22 +37,22 @@ def initialize(response, object)
end

def_delegators :@objects, :<<, :[], :[]=, :last, :size

def method_missing(meth, *args, &block)
if meth.to_s =~ /^(.*)_page$/
get_url_page($1, *args, &block)
else
super
end
end

def get_url_page(which, *args, &block)
url = instance_variable_get("@#{which}_url")
Thingiverse::Pagination.new(Thingiverse::Connection.get(url), @object) if url
end

def each(&block)
@objects.each(&block)
end
end
end
end
19 changes: 19 additions & 0 deletions lib/thingiverse/response_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Thingiverse
class ResponseError < StandardError
def initialize(response)
@response = response
end

attr_reader :response

def message
"#{response.code}: #{message_body} #{response.headers['x-error']}".strip
end

def message_body
JSON.parse(response.body)['error']
rescue
response.body
end
end
end
4 changes: 2 additions & 2 deletions lib/thingiverse/tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Tags

def self.find(tag_name)
response = Thingiverse::Connection.get("/tags/#{tag_name}")
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
raise ResponseError.new(response) unless response.success?
self.new response.parsed_response
end

def things(query = {})
Thingiverse::Pagination.new(Thingiverse::Connection.get(things_url, :query => query), Thingiverse::Things)
end
Expand Down
22 changes: 11 additions & 11 deletions lib/thingiverse/things.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Things

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

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

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

thing = Thingiverse::Things.new(response.parsed_response)
end
Expand All @@ -46,7 +46,7 @@ def save
send("#{name}=", value)
end
end

# file_or_string can be a File or a String.
# thingiverse_filename is optional if using a File (the File filename will be used by default) but is required if using a String
def upload(file_or_string, thingiverse_filename=nil)
Expand All @@ -59,11 +59,11 @@ def upload(file_or_string, thingiverse_filename=nil)
else
raise ArgumentError, "file_or_string not of accepted type. Expected File or String. Actual: #{file_or_string.class}"
end

raise ArgumentError, "Unable to determine filename" if thingiverse_filename.to_s == ""

response = Thingiverse::Connection.post("/things/#{id}/files", :body => {:filename => thingiverse_filename}.to_json)
raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
raise ResponseError.new(response) unless response.success?

parsed_response = JSON.parse(response.body)
action = parsed_response["action"]
Expand Down Expand Up @@ -96,7 +96,7 @@ def upload(file_or_string, thingiverse_filename=nil)
if c.response_code == 303
# finalize it
response = Thingiverse::Connection.post(query['success_action_redirect'])
raise "#{response.code}: #{JSON.parse(response.body)['error']} #{response.headers['x-error']}" unless response.success?
raise ResponseError.new(response) unless response.success?
Thingiverse::Files.new(response.parsed_response)
else
raise "#{c.response_code}: #{c.body_str}"
Expand All @@ -108,7 +108,7 @@ def publish
raise "Cannot publish until thing is saved"
else
response = Thingiverse::Connection.post("/things/#{id}/publish")
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
raise ResponseError.new(response) unless response.success?

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

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

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

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

self.new(response.parsed_response)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/thingiverse/users.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module Thingiverse
class Users
include Thingiverse::DynamicAttributes

def self.find(user_name)
response = Thingiverse::Connection.get("/users/#{user_name}")
raise "#{response.code}: #{JSON.parse(response.body)['error']}" unless response.success?
raise ResponseError.new(response) unless response.success?
self.new response.parsed_response
end
end
Expand Down

0 comments on commit b0d02de

Please sign in to comment.