From 096a33dd7733351fe260921cfc0e8407edfbb333 Mon Sep 17 00:00:00 2001 From: Lucas Volle Date: Fri, 22 Jan 2021 12:55:21 -0500 Subject: [PATCH 1/2] fix: correct api error message parsing fix: raise exceptions with formatted string --- lib/mapbox.rb | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/mapbox.rb b/lib/mapbox.rb index eb704e0..c194e78 100644 --- a/lib/mapbox.rb +++ b/lib/mapbox.rb @@ -104,8 +104,8 @@ def self.general_api_error(rcode, rbody) def self.handle_api_error(rcode, rbody) begin - error_obj = JSON.parse(rbody) - error = error_obj[:error] or raise StandardError.new # escape from parsing + error = JSON.parse(rbody) + raise StandardError.new if !error.has_key?("message") # escape from parsing rescue JSON::ParserError, StandardError raise general_api_error(rcode, rbody) @@ -113,26 +113,25 @@ def self.handle_api_error(rcode, rbody) case rcode when 400, 404 - raise invalid_request_error error, rcode, rbody, error_obj + raise invalid_request_error error, rcode when 401 - raise authentication_error error, rcode, rbody, error_obj + raise authentication_error error, rcode else - raise api_error error, rcode, rbody, error_obj + raise api_error error, rcode end end - def self.invalid_request_error(error, rcode, rbody, error_obj) - StandardError.new(error[:message], error[:param], rcode, - rbody, error_obj) + def self.invalid_request_error(error, rcode) + StandardError.new("Request Error: #{rcode} - #{error["message"]}") end - def self.authentication_error(error, rcode, rbody, error_obj) - AuthenticationError.new(error[:message], rcode, rbody, error_obj) + def self.authentication_error(error, rcode) + AuthenticationError.new("Authentication Error: #{rcode} - #{error["message"]}") end - def self.api_error(error, rcode, rbody, error_obj) - StandardError.new(error[:message], rcode, rbody, error_obj) + def self.api_error(error, rcode) + StandardError.new("API Error: #{rcode} - #{error["message"]}") end def self.handle_restclient_error(e, api_base_url=nil) From 46479c8ca6141944a2bc38fd166129ba3a60288e Mon Sep 17 00:00:00 2001 From: Lucas Volle Date: Mon, 25 Jan 2021 08:53:28 -0500 Subject: [PATCH 2/2] style: replace `if !` with `unless` --- lib/mapbox.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mapbox.rb b/lib/mapbox.rb index c194e78..b05c685 100644 --- a/lib/mapbox.rb +++ b/lib/mapbox.rb @@ -105,7 +105,7 @@ def self.general_api_error(rcode, rbody) def self.handle_api_error(rcode, rbody) begin error = JSON.parse(rbody) - raise StandardError.new if !error.has_key?("message") # escape from parsing + raise StandardError.new unless error.has_key?("message") # escape from parsing rescue JSON::ParserError, StandardError raise general_api_error(rcode, rbody)