Skip to content

Commit

Permalink
Merge pull request #577 from rspeicher/rs-error-code
Browse files Browse the repository at this point in the history
Support error_code returned by specific API endpoints
  • Loading branch information
NARKOZ authored Jul 18, 2020
2 parents b344165 + b96744c commit 4cbee0c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
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

0 comments on commit 4cbee0c

Please sign in to comment.