From 727b28fa06b871d4fe9a4000e396c92d4c8a839e Mon Sep 17 00:00:00 2001 From: Phil Dibowitz Date: Tue, 15 Apr 2025 11:31:30 -0700 Subject: [PATCH] Fix error raising Currently if you get a 429 (or any other we theoretically catch), we won't catch it because we're doing string comparisons on integers, and thus we jsut get a JSON::ParserError. This fixes that so we get the expected error classes. In addition it adds a new unknown exception that gives the use some extra information. Signed-off-by: Phil Dibowitz --- lib/discogs.rb | 1 + lib/discogs/wrapper.rb | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lib/discogs.rb b/lib/discogs.rb index 63a3d85..40b8ef1 100644 --- a/lib/discogs.rb +++ b/lib/discogs.rb @@ -16,6 +16,7 @@ class Discogs::UnknownResource < StandardError; end class Discogs::InternalServerError < StandardError; end class Discogs::AuthenticationError < StandardError; end class Discogs::RateLimitError < StandardError; end +class Discogs::UnknownServerError < StandardError; end # Loading sequence. require File.dirname(__FILE__) + "/discogs/wrapper" diff --git a/lib/discogs/wrapper.rb b/lib/discogs/wrapper.rb index a7740e0..34edf21 100644 --- a/lib/discogs/wrapper.rb +++ b/lib/discogs/wrapper.rb @@ -742,10 +742,14 @@ def query_and_build(path, params={}, method=:get, body=nil) def query_api(path, params={}, method=:get, body=nil) response = make_request(path, params, method, body) - raise_unknown_resource(path) if response.code == "404" - raise_authentication_error(path) if response.code == "401" - raise_rate_limit_error(path) if response.code == "429" - raise_internal_server_error if response.code == "500" + raise_unknown_resource(path) if response.code == 404 + raise_authentication_error(path) if response.code == 401 + raise_rate_limit_error(path) if response.code == 429 + raise_internal_server_error if response.code == 500 + if response.code >= 300 + puts response.inspect + raise_unknown_server_error(response.code) + end # Unzip the response data, or just read it in directly # if the API responds without gzipping. @@ -853,6 +857,10 @@ def raise_internal_server_error raise Discogs::InternalServerError, "The API server cannot complete the request" end + def raise_unknown_server_error(code="") + raise Discogs::UnknownServerError, "Server returned unexpected response: #{code}" + end + def raise_authentication_error(path="") raise Discogs::AuthenticationError, "Authentication is required for this resource: #{path}" end