Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/uber/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -134,8 +137,8 @@ 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
raise Uber::Error.from_error error
end

def request_headers(method, path, params = {}, signature_params = params)
Expand Down
10 changes: 9 additions & 1 deletion lib/uber/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ 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])
error_base_klass = errors[code] || self
error_base_klass.new(message, error.response[:headers], code)
end

# @return [Hash]
def errors
@errors ||= {
Expand All @@ -44,11 +50,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
Expand Down
2 changes: 1 addition & 1 deletion lib/uber/models/estimate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down