Commit dea8c5cb authored by Andy Soiron's avatar Andy Soiron Committed by Luke Duncalfe

Add HTTP status code to Jira connect client logs

parent baa9281a
...@@ -127,16 +127,21 @@ module Atlassian ...@@ -127,16 +127,21 @@ module Atlassian
def handle_response(response, name, &block) def handle_response(response, name, &block)
data = response.parsed_response data = response.parsed_response
case response.code if [200, 202].include?(response.code)
when 200 then yield data yield data
when 400 then { 'errorMessages' => data.map { |e| e['message'] } }
when 401 then { 'errorMessages' => ['Invalid JWT'] }
when 403 then { 'errorMessages' => ["App does not support #{name}"] }
when 413 then { 'errorMessages' => ['Data too large'] + data.map { |e| e['message'] } }
when 429 then { 'errorMessages' => ['Rate limit exceeded'] }
when 503 then { 'errorMessages' => ['Service unavailable'] }
else else
{ 'errorMessages' => ['Unknown error'], 'response' => data } message = case response.code
when 400 then { 'errorMessages' => data.map { |e| e['message'] } }
when 401 then { 'errorMessages' => ['Invalid JWT'] }
when 403 then { 'errorMessages' => ["App does not support #{name}"] }
when 413 then { 'errorMessages' => ['Data too large'] + data.map { |e| e['message'] } }
when 429 then { 'errorMessages' => ['Rate limit exceeded'] }
when 503 then { 'errorMessages' => ['Service unavailable'] }
else
{ 'errorMessages' => ['Unknown error'], 'response' => data }
end
message.merge('responseCode' => response.code)
end end
end end
......
...@@ -127,11 +127,19 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -127,11 +127,19 @@ RSpec.describe Atlassian::JiraConnect::Client do
end end
end end
context 'the response is 202 accepted' do
let(:response) { double(code: 202, parsed_response: :foo) }
it 'yields to the block' do
expect(processed).to eq [:data, :foo]
end
end
context 'the response is 400 bad request' do context 'the response is 400 bad request' do
let(:response) { double(code: 400, parsed_response: errors) } let(:response) { double(code: 400, parsed_response: errors) }
it 'extracts the errors messages' do it 'extracts the errors messages' do
expect(processed).to eq('errorMessages' => %w(X Y)) expect(processed).to eq('errorMessages' => %w(X Y), 'responseCode' => 400)
end end
end end
...@@ -139,7 +147,7 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -139,7 +147,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
let(:response) { double(code: 401, parsed_response: nil) } let(:response) { double(code: 401, parsed_response: nil) }
it 'reports that our JWT is wrong' do it 'reports that our JWT is wrong' do
expect(processed).to eq('errorMessages' => ['Invalid JWT']) expect(processed).to eq('errorMessages' => ['Invalid JWT'], 'responseCode' => 401)
end end
end end
...@@ -147,7 +155,7 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -147,7 +155,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
let(:response) { double(code: 403, parsed_response: nil) } let(:response) { double(code: 403, parsed_response: nil) }
it 'reports that the App is misconfigured' do it 'reports that the App is misconfigured' do
expect(processed).to eq('errorMessages' => ['App does not support foo']) expect(processed).to eq('errorMessages' => ['App does not support foo'], 'responseCode' => 403)
end end
end end
...@@ -155,7 +163,7 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -155,7 +163,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
let(:response) { double(code: 413, parsed_response: errors) } let(:response) { double(code: 413, parsed_response: errors) }
it 'extracts the errors messages' do it 'extracts the errors messages' do
expect(processed).to eq('errorMessages' => ['Data too large', 'X', 'Y']) expect(processed).to eq('errorMessages' => ['Data too large', 'X', 'Y'], 'responseCode' => 413)
end end
end end
...@@ -163,7 +171,7 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -163,7 +171,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
let(:response) { double(code: 429, parsed_response: nil) } let(:response) { double(code: 429, parsed_response: nil) }
it 'reports that we exceeded the rate limit' do it 'reports that we exceeded the rate limit' do
expect(processed).to eq('errorMessages' => ['Rate limit exceeded']) expect(processed).to eq('errorMessages' => ['Rate limit exceeded'], 'responseCode' => 429)
end end
end end
...@@ -171,7 +179,7 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -171,7 +179,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
let(:response) { double(code: 503, parsed_response: nil) } let(:response) { double(code: 503, parsed_response: nil) }
it 'reports that the service is unavailable' do it 'reports that the service is unavailable' do
expect(processed).to eq('errorMessages' => ['Service unavailable']) expect(processed).to eq('errorMessages' => ['Service unavailable'], 'responseCode' => 503)
end end
end end
...@@ -179,7 +187,7 @@ RSpec.describe Atlassian::JiraConnect::Client do ...@@ -179,7 +187,7 @@ RSpec.describe Atlassian::JiraConnect::Client do
let(:response) { double(code: 1000, parsed_response: :something) } let(:response) { double(code: 1000, parsed_response: :something) }
it 'reports that this was unanticipated' do it 'reports that this was unanticipated' do
expect(processed).to eq('errorMessages' => ['Unknown error'], 'response' => :something) expect(processed).to eq('errorMessages' => ['Unknown error'], 'responseCode' => 1000, 'response' => :something)
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment