From 1d6cea62e7127abf554037bf16b11f37e5585c1c Mon Sep 17 00:00:00 2001 From: Ankur Goel Date: Sun, 4 Sep 2016 23:43:38 +0530 Subject: [PATCH 1/2] error handling #wip --- lib/uber/client.rb | 10 ++++++++-- lib/uber/error.rb | 9 ++++++++- lib/uber/models/estimate.rb | 2 +- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/uber/client.rb b/lib/uber/client.rb index 45d4de7..f9e2f2e 100644 --- a/lib/uber/client.rb +++ b/lib/uber/client.rb @@ -59,6 +59,9 @@ def middleware faraday.request :url_encoded # Parse JSON response bodies faraday.response :parse_json + faraday.response :logger + # Include Faraday::Response::RaiseError + faraday.response :raise_error if defined?(Faraday::Response::RaiseError) # Use instrumentation if available faraday.use :instrumentation if defined?(FaradayMiddleware::Instrumentation) # Set default HTTP adapter @@ -134,8 +137,11 @@ def request(method, path, params = {}, headers = {}) connection.send(method.to_sym, path, params) { |request| request.headers.update(headers) }.env rescue Faraday::Error::TimeoutError, Timeout::Error => error raise(Uber::Error::RequestTimeout.new(error)) - rescue Faraday::Error::ClientError, JSON::ParserError => error - fail(Uber::Error.new(error)) + rescue Faraday::Error::ClientError, Faraday::Error::ResourceNotFound, Faraday::Error::ConnectionFailed, JSON::ParserError => error + # TODO: check if I can just handle Faraday::Error + #debugger + raise Uber::Error.from_error error + # fail(Uber::Error.new(error)) end def request_headers(method, path, params = {}, signature_params = params) diff --git a/lib/uber/error.rb b/lib/uber/error.rb index 7949ce9..7adef35 100644 --- a/lib/uber/error.rb +++ b/lib/uber/error.rb @@ -28,6 +28,11 @@ def from_response(response) new(message, response.response_headers, code) end + def from_error(error) + message, code = parse_error((JSON.parse(error.response[:body]) rescue nil), error.response[:status]) + new(message, error.response[:headers], code) + end + # @return [Hash] def errors @errors ||= { @@ -44,11 +49,13 @@ def errors private - def parse_error(body) + def parse_error(body, status_code=nil) if body.nil? ['', nil] elsif body[:error] [body[:error], nil] + elsif body[:message] || body['message'] + [body[:message] || body['message'], status_code] elsif body[:errors] extract_message_from_errors(body) end diff --git a/lib/uber/models/estimate.rb b/lib/uber/models/estimate.rb index 5e9c973..7aadc2b 100644 --- a/lib/uber/models/estimate.rb +++ b/lib/uber/models/estimate.rb @@ -18,7 +18,7 @@ def humanized_estimate end class Price < Base - attr_accessor :surge_confirmation_href, :surge_confirmation_id, :high_estimate, :low_estimate, :minimum, :surge_multiplier, :display, :currency_code + attr_accessor :surge_confirmation_href, :surge_confirmation_id, :high_estimate, :low_estimate, :minimum, :surge_multiplier, :display, :currency_code, :fare_breakdown end class Trip < Base From a680e47dc8ca56d898c639517ce3eef7f1434e14 Mon Sep 17 00:00:00 2001 From: Ankur Goel Date: Sun, 4 Sep 2016 23:52:13 +0530 Subject: [PATCH 2/2] Use custom errors --- lib/uber/client.rb | 3 --- lib/uber/error.rb | 3 ++- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/uber/client.rb b/lib/uber/client.rb index f9e2f2e..557c4a9 100644 --- a/lib/uber/client.rb +++ b/lib/uber/client.rb @@ -138,10 +138,7 @@ def request(method, path, params = {}, headers = {}) rescue Faraday::Error::TimeoutError, Timeout::Error => error raise(Uber::Error::RequestTimeout.new(error)) rescue Faraday::Error::ClientError, Faraday::Error::ResourceNotFound, Faraday::Error::ConnectionFailed, JSON::ParserError => error - # TODO: check if I can just handle Faraday::Error - #debugger raise Uber::Error.from_error error - # fail(Uber::Error.new(error)) end def request_headers(method, path, params = {}, signature_params = params) diff --git a/lib/uber/error.rb b/lib/uber/error.rb index 7adef35..a9a1a8b 100644 --- a/lib/uber/error.rb +++ b/lib/uber/error.rb @@ -30,7 +30,8 @@ def from_response(response) def from_error(error) message, code = parse_error((JSON.parse(error.response[:body]) rescue nil), error.response[:status]) - new(message, error.response[:headers], code) + error_base_klass = errors[code] || self + error_base_klass.new(message, error.response[:headers], code) end # @return [Hash]