Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support error_code returned by specific API endpoints #577

Merged
merged 1 commit into from
Jul 18, 2020
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
11 changes: 11 additions & 0 deletions lib/gitlab/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def response_message
@response.parsed_response.message
end

# Additional error context returned by some API endpoints
#
# @return [String]
def error_code
if @response.respond_to?(:error_code)
@response.error_code
else
''
end
end

private

# Human friendly message.
Expand Down
1 change: 1 addition & 0 deletions spec/fixtures/cherry_pick_commit_failure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"Sorry, we cannot cherry-pick this commit automatically. This commit may already have been cherry-picked, or a more recent commit may have updated some of its content.","error_code":"conflict"}
40 changes: 26 additions & 14 deletions spec/gitlab/client/commits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,32 @@
end

describe '.cherry_pick_commit' do
before do
stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'project_commit').with(body: { branch: 'master' })
@cherry_pick_commit = Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master')
end

it 'gets the correct resource' do
expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick')
.with(body: { branch: 'master' }))
.to have_been_made
end

it 'returns the correct response' do
expect(@cherry_pick_commit).to be_a Gitlab::ObjectifiedHash
expect(@cherry_pick_commit.id).to eq('6104942438c14ec7bd21c6cd5bd995272b3faff6')
context 'on success' do
before do
stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'project_commit').with(body: { branch: 'master' })
@cherry_pick_commit = Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master')
end

it 'gets the correct resource' do
expect(a_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick')
.with(body: { branch: 'master' }))
.to have_been_made
end

it 'returns the correct response' do
expect(@cherry_pick_commit).to be_a Gitlab::ObjectifiedHash
expect(@cherry_pick_commit.id).to eq('6104942438c14ec7bd21c6cd5bd995272b3faff6')
end
end

context 'on failure' do
it 'includes the error_code' do
stub_post('/projects/3/repository/commits/6104942438c14ec7bd21c6cd5bd995272b3faff6/cherry_pick', 'cherry_pick_commit_failure', 400)

expect { Gitlab.cherry_pick_commit(3, '6104942438c14ec7bd21c6cd5bd995272b3faff6', 'master') }.to raise_error(Gitlab::Error::BadRequest) do |ex|
expect(ex.error_code).to eq('conflict')
end
end
end
end

Expand Down
35 changes: 35 additions & 0 deletions spec/gitlab/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,39 @@
expect(described_class.new(response_double).send(:build_error_message)).to match(/Retry text/)
end
end

describe '#error_code' do
it 'returns the value when available' do
headers = { 'content-type' => 'application/json' }
response_double = double(
'response',
body: 'Retry later',
to_s: 'Retry text',
parsed_response: { message: 'Retry hash' },
code: 400,
error_code: 'conflict',
options: {},
headers: headers,
request: @request_double
)

expect(described_class.new(response_double).error_code).to eq 'conflict'
end

it 'returns nothing when unavailable' do
headers = { 'content-type' => 'application/json' }
response_double = double(
'response',
body: 'Retry later',
to_s: 'Retry text',
parsed_response: { message: 'Retry hash' },
code: 400,
options: {},
headers: headers,
request: @request_double
)

expect(described_class.new(response_double).error_code).to eq ''
end
end
end