From b2bb1ed5d1140f7d7ed6b2de565f2c3a0c1de4bd Mon Sep 17 00:00:00 2001 From: Olivier Date: Thu, 30 Nov 2023 19:22:10 -0500 Subject: [PATCH] Error handling --- lib/queenbee.rb | 18 +++++++++--------- lib/queenbee/errors/invalid_request_error.rb | 2 +- lib/queenbee/errors/queenbee_error.rb | 13 ++++++++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/queenbee.rb b/lib/queenbee.rb index f44f337..fcc53c5 100644 --- a/lib/queenbee.rb +++ b/lib/queenbee.rb @@ -110,32 +110,32 @@ def self.handle_api_error(rcode, rbody) raise general_api_error(rcode, rbody) end - case rcode + case rcode.to_i when 400, 404, 422 - raise invalid_request_error "Invalid request", rcode, rbody, error_obj + raise invalid_request_error("Invalid request", rcode, rbody, error_obj).to_s when 401 - raise authentication_error "Unauthorized", rcode, rbody, error_obj + raise authentication_error("Unauthorized", rcode, rbody, error_obj).to_s when 500 - raise api_error "Server returned an error", rcode, rbody, error_obj + raise api_error("Server returned an error", rcode, rbody, error_obj).to_s else - raise api_error "An error has occurred", rcode, rbody, error_obj + raise api_error("An error has occurred", rcode, rbody, error_obj).to_s end end def self.invalid_request_error(message, rcode, rbody, error_obj) - InvalidRequestError.new(message, rcode, rbody, error_obj) + InvalidRequestError.new(message, nil, rcode, rbody, error_obj) end def self.authentication_error(message, rcode, rbody, error_obj) - AuthenticationError.new(message, rcode, rbody, error_obj) + AuthenticationError.new(message, nil, rcode, rbody, error_obj) end def self.api_error(message, rcode, rbody, error_obj) - APIError.new(message, rcode, rbody, error_obj) + APIError.new(message, nil, rcode, rbody, error_obj) end def self.general_api_error(rcode, rbody) APIError.new("Invalid response object from API: #{rbody.inspect} " + - "(HTTP response code was #{rcode})", rcode, rbody) + "(HTTP response code was #{rcode})", nil, rcode, rbody) end end diff --git a/lib/queenbee/errors/invalid_request_error.rb b/lib/queenbee/errors/invalid_request_error.rb index 46a56bf..334dd4c 100644 --- a/lib/queenbee/errors/invalid_request_error.rb +++ b/lib/queenbee/errors/invalid_request_error.rb @@ -7,4 +7,4 @@ def initialize(message, param, http_status=nil, http_body=nil, json_body=nil) @param = param end end -end \ No newline at end of file +end diff --git a/lib/queenbee/errors/queenbee_error.rb b/lib/queenbee/errors/queenbee_error.rb index 082ba0c..a4270e0 100644 --- a/lib/queenbee/errors/queenbee_error.rb +++ b/lib/queenbee/errors/queenbee_error.rb @@ -13,8 +13,15 @@ def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil) end def to_s - status_string = @http_status.nil? ? "" : "(Status #{@http_status}) " - "#{status_string}#{@message}" + [ + status_string, + json_body, + message + ].compact.join(" - ") + end + + def status_string + http_status.nil? ? "" : "(Status #{http_status})" end end -end \ No newline at end of file +end