Skip to content

Commit

Permalink
[10403] 422 No Supplies Selected Error Handling & 503 Service Unavail…
Browse files Browse the repository at this point in the history
…able Error Handling (#4410)

* supplies not selected

* change invalid to 404

* add 503 check

* added service_unavailable exception

* fix client error status code

* fix invalid order test

* test fix

* add get_supplies_503 spec

* test fix

* test fix
  • Loading branch information
voidspooks authored Jun 19, 2020
1 parent fbc1277 commit fd66dad
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 12 deletions.
14 changes: 13 additions & 1 deletion config/locales/exceptions.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,12 @@ en:
detail: Received an an invalid response from the upstream server
code: MDOT_502
status: 502
MDOT_service_unavailable:
<<: *defaults
title: Service Unavailable
detail: The DLC API is currently unavailable
code: MDOT_service_unavailable
status: 503
MDOT_malformed_request:
<<: *defaults
title: Malformed Request
Expand All @@ -1970,13 +1976,19 @@ en:
title: Veteran Not Found
detail: The veteran could not be found
code: MDOT_invalid
status: 422
status: 404
MDOT_supplies_not_found:
<<: *defaults
title: Supplies Not Found
detail: The medical supplies could not be found
code: MDOT_supplies_not_found
status: 404
MDOT_supplies_not_selected:
<<: *defaults
title: Supplies Not Selected
detail: No supplies were selected to order
code: MDOT_supplies_not_selected
status: 422
MDOT_unprocessable_entity:
<<: *defaults
title: Order unable to be processed
Expand Down
10 changes: 9 additions & 1 deletion lib/mdot/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def get_supplies
#
def submit_order(request_body)
request_body.deep_transform_keys! { |key| key.to_s.camelize(:lower) }

if request_body['order'].empty?
raise_backend_exception(
MDOT::ExceptionKey.new('MDOT_supplies_not_selected'),
self.class
)
end

with_monitoring_and_error_handling do
perform(:post, @supplies, request_body, submission_headers).body
end
Expand Down Expand Up @@ -119,7 +127,7 @@ def handle_parsing_error(error)

def handle_client_error(error)
save_error_details(error)
code = error.body['result'].downcase
code = error&.status != 503 ? error.body['result'].downcase : 'service_unavailable'

raise_backend_exception(
MDOT::ExceptionKey.new("MDOT_#{code}"),
Expand Down
61 changes: 51 additions & 10 deletions spec/lib/mdot/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@
end
end

context 'when the DLC API is unavailable' do
it 'raises a 503' do
VCR.use_cassette('mdot/get_supplies_503') do
expect(StatsD).to receive(:increment).once.with(
'api.mdot.get_supplies.fail', tags: [
'error:Common::Client::Errors::ClientError', 'status:503'
]
)
expect(StatsD).to receive(:increment).once.with(
'api.mdot.get_supplies.total'
)
expect { subject.get_supplies }.to raise_error(
MDOT::ServiceException
) do |e|
expect(e.message).to match(/MDOT_service_unavailable/)
end
end
end
end

context 'with a deceased veteran' do
it 'returns a 403' do
VCR.use_cassette('mdot/get_supplies_403') do
Expand Down Expand Up @@ -115,6 +135,31 @@
}
end

let(:invalid_order) do
{
'useVeteranAddress' => true,
'useTemporaryAddress' => false,
'vetEmail' => 'vet1@va.gov',
'order' => [],
'permanentAddress' => {
'street' => '125 SOME RD',
'street2' => 'APT 101',
'city' => 'DENVER',
'state' => 'CO',
'country' => 'United States',
'postalCode' => '111119999'
},
'temporaryAddress' => {
'street' => '17250 w colfax ave',
'street2' => 'a-204',
'city' => 'Golden',
'state' => 'CO',
'country' => 'United States',
'postalCode' => '80401'
}
}
end

context 'with a valid supplies order' do
it 'returns a successful response' do
VCR.use_cassette('mdot/submit_order', VCR::MATCH_EVERYTHING) do
Expand Down Expand Up @@ -144,16 +189,12 @@
end

context 'with an malformed order' do
it 'returns a 400 error' do
VCR.use_cassette('mdot/submit_order_400') do
expect(StatsD).to receive(:increment).once.with(
'api.mdot.submit_order.fail', tags: [
'error:Common::Client::Errors::ClientError', 'status:400'
]
)
expect(StatsD).to receive(:increment).once.with('api.mdot.submit_order.total')
set_mdot_token_for(user)
expect { subject.submit_order({}) }.to raise_error(MDOT::ServiceException)
it 'returns a 422 error' do
set_mdot_token_for(user)
expect { subject.submit_order(invalid_order) }.to raise_error(
MDOT::ServiceException
) do |e|
expect(e.message).to match(/MDOT_supplies_not_selected/)
end
end
end
Expand Down
80 changes: 80 additions & 0 deletions spec/support/vcr_cassettes/mdot/get_supplies_503.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd66dad

Please sign in to comment.