lib/travis/client/session.rb in travis-1.6.3.travis.388.4 vs lib/travis/client/session.rb in travis-1.6.3.travis.389.4
- old
+ new
@@ -54,13 +54,10 @@
def uri=(uri)
clear_cache!
self.connection = Faraday.new(:url => uri, :ssl => ssl) do |faraday|
faraday.request :url_encoded
- faraday.response :json
- faraday.response :follow_redirects
- faraday.response :raise_error
faraday.adapter(*faraday_adapter)
end
end
def faraday_adapter=(adapter)
@@ -171,15 +168,27 @@
end
def raw(verb, url, *args)
url = url.sub(/^\//, '')
- result = instrumented(verb.to_s.upcase, url, *args) { connection.public_send(verb, url, *args) }
- raise Travis::Client::Error, 'SSL error: could not verify peer' if result.status == 0
- result.body
- rescue Faraday::Error::ClientError => e
- handle_error(e)
+ result = instrumented(verb.to_s.upcase, url, *args) do
+ connection.public_send(verb, url, *args) do |request|
+ next if request.path !~ /^https?:/ or request.path.start_with? api_endpoint
+ request.headers.delete("Authorization")
+ end
+ end
+
+ case result.status
+ when 0 then raise Travis::Client::Error, 'SSL error: could not verify peer'
+ when 200..299 then JSON.parse(result.body) rescue result.body
+ when 301, 303 then raw(:get, result.headers['Location'])
+ when 302, 307, 308 then raw(verb, result.headers['Location'])
+ when 404 then raise Travis::Client::NotFound, result.body
+ when 400..499 then raise Travis::Client::Error, result.status
+ when 500..599 then raise Travis::Client::Error, "server error (#{result.status})"
+ else raise Travis::Client::Error, "unhandled status code #{result.status}"
+ end
end
def inspect
"#<#{self.class}: #{uri}>"
end
@@ -236,15 +245,9 @@
data = { type.id_field => data } if type.id? data
id = type.cast_id(data.fetch(type.id_field)) unless type.weak?
entity = id ? cached(type, :id, id) { type.new(self, id) } : type.new(self, nil)
entity.update_attributes(data)
entity
- end
-
- def handle_error(e)
- klass = Travis::Client::NotFound if e.is_a? Faraday::Error::ResourceNotFound
- klass ||= Travis::Client::Error
- raise klass, error_message(e), e.backtrace
end
def error_message(e)
message = e.response[:body].to_str rescue e.message
JSON.parse(message).fetch('error').fetch('message') rescue message