Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Metrics/BlockLength:
Exclude:
- 'spec/**/*'

Metrics/CyclomaticComplexity:
Max: 15

Style/Documentation:
Enabled: false

Expand Down
16 changes: 15 additions & 1 deletion lib/gitlab/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def response_message
#
# @return [String]
def build_error_message
parsed_response = @response.parsed_response
parsed_response = classified_response
message = check_error_keys(parsed_response)
"Server responded with code #{@response.code}, message: " \
"#{handle_message(message)}. " \
Expand All @@ -54,6 +54,17 @@ def check_error_keys(resp)
key ? resp.send(key) : resp
end

# Parse the body based on the classification of the body content type
#
# @return parsed response
def classified_response
if @response.respond_to?('headers')
@response.headers['content-type'] == 'text/plain' ? { message: @response.to_s } : @response.parsed_response
else
@response.parsed_response
end
end

# Handle error response message in case of nested hashes
def handle_message(message)
case message
Expand Down Expand Up @@ -90,6 +101,9 @@ class Conflict < ResponseError; end
# Raised when API endpoint returns the HTTP status code 422.
class Unprocessable < ResponseError; end

# Raised when API endpoint returns the HTTP status code 429.
class TooManyRequests < ResponseError; end

# Raised when API endpoint returns the HTTP status code 500.
class InternalServerError < ResponseError; end

Expand Down
1 change: 1 addition & 0 deletions lib/gitlab/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def validate(response)
when 405 then Error::MethodNotAllowed
when 409 then Error::Conflict
when 422 then Error::Unprocessable
when 429 then Error::TooManyRequests
when 500 then Error::InternalServerError
when 502 then Error::BadGateway
when 503 then Error::ServiceUnavailable
Expand Down
18 changes: 18 additions & 0 deletions spec/gitlab/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,21 @@
end
end
end

describe Gitlab::Error::ResponseError do
before do
@request_double = double('request', base_uri: 'https://gitlab.com/api/v3', path: '/foo', options: {})
end

it 'Builds an error message from text' do
headers = { 'content-type' => 'text/plain' }
response_double = double('response', body: 'Retry later', to_s: 'Retry text', parsed_response: { message: 'Retry hash' }, code: 429, options: {}, headers: headers, request: @request_double)
expect(described_class.new(response_double).send(:build_error_message)).to match(/Retry text/)
end

it 'Builds an error message from parsed json' do
headers = { 'content-type' => 'application/json' }
response_double = double('response', body: 'Retry later', to_s: 'Retry text', parsed_response: { message: 'Retry hash' }, code: 429, options: {}, headers: headers, request: @request_double)
expect(described_class.new(response_double).send(:build_error_message)).to match(/Retry hash/)
end
end