Skip to content

Commit

Permalink
API-35829-error_handling (#16522)
Browse files Browse the repository at this point in the history
* Adds error handling for new synchronous endpoint. Iterates through errors and adds them to the ecss_response on the claim. Adds to the retry? method to check the evss_response for the error key.

* Addressese PR suggestions

* REmoves error message, and uses evss_response on the claim instead.

* Adds a save rigt after updating the status on the claim

* Adds several tests

* Alters a test to more accurately test the scenario IRL
  • Loading branch information
stiehlrod authored Apr 29, 2024
1 parent 54f57f8 commit c42f6d2
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 37 deletions.
30 changes: 26 additions & 4 deletions modules/claims_api/app/sidekiq/claims_api/service_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ def save_auto_claim!(auto_claim, status)
auto_claim.save!
end

def set_evss_response(auto_claim, error)
auto_claim.status = ClaimsApi::AutoEstablishedClaim::ERRORED
auto_claim.save!

auto_claim.evss_response = []
error_messages = get_error_message(error)
messages = error_messages&.dig(0, :messages).presence ? error_messages[:messages] : [error_messages]

messages.flatten.uniq.each do |error_message|
error_key = get_error_key(error_message)
error_text = get_error_text(error_message)
auto_claim.evss_response <<
{ 'key' => error_key,
'severity' => 'FATAL',
'text' => error_text }
end

save_auto_claim!(auto_claim, auto_claim.status)
end

def get_error_message(error)
if error.respond_to? :original_body
error.original_body
Expand All @@ -65,13 +85,13 @@ def get_error_message(error)
def get_error_key(error_message)
return error_message if error_message.is_a? String

error_message.dig(:messages, 0, :key) || error_message
error_message&.dig(:messages, 0, :key) || error_message&.dig(:key)
end

def get_error_text(error_message)
return error_message if error_message.is_a? String

error_message.dig(:messages, 0, :text) || error_message
error_message&.dig(:messages, 0, :text) || error_message&.dig(:text)
end

def get_error_status_code(error)
Expand All @@ -90,8 +110,10 @@ def get_original_status_code(error)
end
end

def will_retry?(error)
msg = if error.respond_to? :original_body
def will_retry?(auto_claim, error)
msg = if auto_claim.evss_response.present?
auto_claim.evss_response&.dig(0, 'key')
elsif error.respond_to? :original_body
get_error_key(error.original_body)
else
''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,27 @@ def perform(claim_id) # rubocop:disable Metrics/MethodLength
# now upload to benefits documents
start_bd_uploader_job(auto_claim) if auto_claim.status != errored_state_value
rescue Faraday::ParsingError, Faraday::TimeoutError => e
set_errored_state_on_claim(auto_claim)
set_evss_response(auto_claim, e)
error_status = get_error_status_code(e)
error_message = get_error_message(e)

log_job_progress(claim_id,
"Docker container job errored #{e.class}: #{error_status} #{error_message}")
"Docker container job errored #{e.class}: #{error_status} #{auto_claim&.evss_response}")

log_exception_to_sentry(e)

raise e
rescue ::Common::Exceptions::BackendServiceException => e
set_errored_state_on_claim(auto_claim)
set_evss_response(auto_claim, e)
error_message = get_error_message(e)

log_job_progress(claim_id,
"Docker container job errored #{e.class}: #{error_message}")
"Docker container job errored #{e.class}: #{auto_claim&.evss_response}")
log_exception_to_sentry(e)
# if will_retry?
if will_retry?(e)
if will_retry?(auto_claim, e)
raise e
else # form526.submit.noRetryError OR form526.InProcess error retruned
{}
end
rescue => e
set_errored_state_on_claim(auto_claim)
set_evss_response(auto_claim, e)
set_evss_response(auto_claim, e) if auto_claim.evss_response.blank?
log_job_progress(claim_id,
"Docker container job errored #{e.class}: #{e&.detailed_message}")
log_exception_to_sentry(e)
Expand All @@ -78,21 +71,6 @@ def queue_flash_updater(flashes, auto_claim_id)
ClaimsApi::FlashUpdater.perform_async(flashes, auto_claim_id)
end

def set_evss_response(auto_claim, error)
error_message = get_error_message(error)
error_key = get_error_key(error_message)
error_text = get_error_text(error_message)

auto_claim.status = ClaimsApi::AutoEstablishedClaim::ERRORED
auto_claim.evss_response = [
{ 'key' => error_key,
'severity' => 'FATAL',
'text' => error_text }
]

save_auto_claim!(auto_claim, auto_claim.status)
end

def start_bd_uploader_job(auto_claim)
bd_service.perform_async(auto_claim.id)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,20 @@ def perform(claim_id, middle_initial) # rubocop:disable Metrics/MethodLength

start_docker_container_job(auto_claim&.id) if auto_claim.status != errored_state_value
rescue Faraday::ParsingError, Faraday::TimeoutError => e
set_errored_state_on_claim(auto_claim)
error_message = get_error_message(e)
set_evss_response(auto_claim, e)
error_status = get_error_status_code(e)

log_job_progress(claim_id,
"526EZ PDF generator faraday error #{e.class}: #{error_status} #{error_message}")
"526EZ PDF generator faraday error #{e.class}: #{error_status} #{auto_claim&.evss_response}")
log_exception_to_sentry(e)

raise e
rescue ::Common::Exceptions::BackendServiceException => e
set_errored_state_on_claim(auto_claim)
error_message = get_error_message(e)
set_evss_response(auto_claim, e)
error_status = get_error_status_code(e)

log_job_progress(claim_id,
"526EZ PDF generator errored #{e.class}: #{error_status} #{error_message}")
"526EZ PDF generator errored #{e.class}: #{error_status} #{auto_claim&.evss_response}")
log_exception_to_sentry(e)

raise e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
end
end

it 'does retry when form526.submit.establshClaim.serviceError gets retruned' do
it 'does retry when form526.submit.establshClaim.serviceError gets returned' do
body = {
messages: [
{ key: 'form526.submit.establshClaim.serviceError',
Expand All @@ -109,6 +109,77 @@
service.perform(claim.id)
end.to raise_error(Common::Exceptions::BackendServiceException)
end

it "does not retry when the message key is 'in progress' and are in an array" do
body = {
messages: [{ key: 'form526.InProcess', severity: 'FATAL', text: 'Form 526 is already in-process' }]
}

allow_any_instance_of(ClaimsApi::EVSSService::Base).to(
receive(:submit).and_raise(Common::Exceptions::BackendServiceException.new(
'form526.submit.establshClaim.serviceError', {}, nil, body
))
)
service.perform(claim_with_evss_response.id)

claim_with_evss_response.reload
expect(claim_with_evss_response.status).to eq('errored')
expect(claim_with_evss_response.evss_response).to eq([{ 'key' => 'form526.InProcess', 'severity' => 'FATAL',
'text' => 'Form 526 is already in-process' }])
end

it 'does retry when the message indicates a birls error and is in an array' do
body = [{ key: 'header.va_eauth_birlsfilenumber.Invalid', severity: 'ERROR',
text: 'Size must be between 8 and 9' }]

allow_any_instance_of(ClaimsApi::EVSSService::Base).to(
receive(:submit).and_raise(Common::Exceptions::BackendServiceException.new(
'form526.submit.establshClaim.serviceError', {}, nil, body
))
)
claim_with_evss_response.reload
expect do
service.perform(claim.id)
end.to raise_error(Common::Exceptions::BackendServiceException)
expect(claim_with_evss_response.status).to eq('errored')
end

it 'does retry when the message indicates a birls error and is NOT in an array' do
body = { key: 'header.va_eauth_birlsfilenumber', severity: 'ERROR',
text: 'Size must be between 8 and 9' }

allow_any_instance_of(ClaimsApi::EVSSService::Base).to(
receive(:submit).and_raise(Common::Exceptions::BackendServiceException.new(
'form526.submit.establshClaim.serviceError', {}, nil, body
))
)
claim_with_evss_response.reload
expect do
service.perform(claim.id)
end.to raise_error(Common::Exceptions::BackendServiceException)
expect(claim_with_evss_response.status).to eq('errored')
end

it 'does not retry when the message indicates a birls error and is an array of many messages' do
body = {
messages: [
{ key: 'header.va_eauth_birlsfilenumber', severity: 'ERROR',
text: 'Size must be between 8 and 9' },
{ key: 'form526.InProcess', severity: 'FATAL', text: 'Form 526 is already in-process' }
]
}

allow_any_instance_of(ClaimsApi::EVSSService::Base).to(
receive(:submit).and_raise(Common::Exceptions::BackendServiceException.new(
'form526.submit.establshClaim.serviceError', {}, nil, body
))
)
claim_with_evss_response.reload
expect do
service.perform(claim.id)
end.to raise_error(Common::Exceptions::BackendServiceException)
expect(claim_with_evss_response.status).to eq('errored')
end
end

context 'errored submission' do
Expand Down

0 comments on commit c42f6d2

Please sign in to comment.