-
Notifications
You must be signed in to change notification settings - Fork 166
Handle StandardError without response in GetUspsProofingResultsJob #7049
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -30,6 +30,8 @@ def enrollment_analytics_attributes(enrollment, complete:) | |||||
| end | ||||||
|
|
||||||
| def response_analytics_attributes(response) | ||||||
| return { response_present: false } unless response.present? | ||||||
|
|
||||||
| { | ||||||
| fraud_suspected: response['fraudSuspected'], | ||||||
| primary_id_type: response['primaryIdType'], | ||||||
|
|
@@ -44,6 +46,7 @@ def response_analytics_attributes(response) | |||||
| proofing_state: response['proofingState'], | ||||||
| scan_count: response['scanCount'], | ||||||
| response_message: response['responseMessage'], | ||||||
| response_present: true, | ||||||
| } | ||||||
| end | ||||||
|
|
||||||
|
|
@@ -108,6 +111,7 @@ def check_enrollments(enrollments) | |||||
| rescue Faraday::BadRequestError => err | ||||||
| handle_bad_request_error(err, enrollment) | ||||||
| rescue StandardError => err | ||||||
| NewRelic::Agent.notice_error(err) | ||||||
| handle_standard_error(err, enrollment) | ||||||
| end | ||||||
|
|
||||||
|
|
@@ -145,10 +149,16 @@ def handle_bad_request_error(err, enrollment) | |||||
| end | ||||||
|
|
||||||
| def handle_standard_error(err, enrollment) | ||||||
| response_attributes = if err.respond_to?(:response) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm trying to understand when a Would an alternative be to exclude
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Despite my prior comment, based on the errors being logged in NewRelic, it would seem that the issue isn't so much whether † The reason being that the exceptions are occurring inside
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok so digging a bit more, I now understand that the errors we're handling here are other Faraday exceptions besides the I think that helps clarify what's actually happening, though (separately) we may want to revisit how we're dealing with Faraday-specific errors, if we're handling some but not all explicitly. For example, every Faraday error extends
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah that would be nice for observability too. I expect we'll see occasional Faraday errors and it won't be out of the ordinary. Non-Faraday standard errors should probably be looked at each time though
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ticket to address this thread: https://cm-jira.usa.gov/browse/LG-7706 |
||||||
| response_analytics_attributes(err.response) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Non-blocking but something to consider fixing up:) The properties that we care about in I'd suggest we either audit the responses we're passing to
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, if we may be expecting
Suggested change
diff --git a/app/jobs/get_usps_proofing_results_job.rb b/app/jobs/get_usps_proofing_results_job.rb
index 36807bab8..dc18dd742 100644
--- a/app/jobs/get_usps_proofing_results_job.rb
+++ b/app/jobs/get_usps_proofing_results_job.rb
@@ -150,7 +150,7 @@ class GetUspsProofingResultsJob < ApplicationJob
def handle_standard_error(err, enrollment)
response_attributes = if err.respond_to?(:response)
- response_analytics_attributes(err.response)
+ response_analytics_attributes(err.response&.[](:body))
else
response_analytics_attributes(nil)
end
diff --git a/app/services/usps_in_person_proofing/mock/fixtures.rb b/app/services/usps_in_person_proofing/mock/fixtures.rb
index 515ee8945..39cc0b5b7 100644
--- a/app/services/usps_in_person_proofing/mock/fixtures.rb
+++ b/app/services/usps_in_person_proofing/mock/fixtures.rb
@@ -25,8 +25,8 @@ module UspsInPersonProofing
load_response_fixture('request_enroll_failed_response.json')
end
- def self.request_enroll_internal_failure_response
- load_response_fixture('request_enroll_internal_failure_response.json')
+ def self.request_internal_failure_response
+ load_response_fixture('request_internal_failure_response.json')
end
def self.request_enroll_invalid_response
diff --git a/app/services/usps_in_person_proofing/mock/proofer.rb b/app/services/usps_in_person_proofing/mock/proofer.rb
index 8005697a1..16822cb60 100644
--- a/app/services/usps_in_person_proofing/mock/proofer.rb
+++ b/app/services/usps_in_person_proofing/mock/proofer.rb
@@ -13,7 +13,7 @@ module UspsInPersonProofing
raise Faraday::BadRequestError.new('Bad request error', response)
when 'usps server error'
# usps 500 response
- body = JSON.parse(Fixtures.request_enroll_internal_failure_response)
+ body = JSON.parse(Fixtures.request_internal_failure_response)
response = { body: body, status: 500 }
raise Faraday::ServerError.new('Internal server error', response)
when 'usps invalid response'
diff --git a/app/services/usps_in_person_proofing/mock/responses/request_enroll_internal_failure_response.json b/app/services/usps_in_person_proofing/mock/responses/request_internal_failure_response.json
similarity index 100%
rename from app/services/usps_in_person_proofing/mock/responses/request_enroll_internal_failure_response.json
rename to app/services/usps_in_person_proofing/mock/responses/request_internal_failure_response.json
diff --git a/spec/jobs/get_usps_proofing_results_job_spec.rb b/spec/jobs/get_usps_proofing_results_job_spec.rb
index 6f5425beb..015f9d00e 100644
--- a/spec/jobs/get_usps_proofing_results_job_spec.rb
+++ b/spec/jobs/get_usps_proofing_results_job_spec.rb
@@ -56,9 +56,7 @@ RSpec.shared_examples 'enrollment with a status update' do |passed:, status:, re
end
end
-RSpec.shared_examples 'enrollment encountering an exception' do |exception_class: nil,
- exception_message: nil,
- reason: 'Request exception'|
+RSpec.shared_examples 'enrollment encountering an exception' do |**event_attributes|
it 'logs an error message and leaves the enrollment and profile pending' do
job.perform(Time.zone.now)
pending_enrollment.reload
@@ -67,11 +65,9 @@ RSpec.shared_examples 'enrollment encountering an exception' do |exception_class
expect(pending_enrollment.profile.active).to eq(false)
expect(job_analytics).to have_logged_event(
'GetUspsProofingResultsJob: Exception raised',
- reason: reason,
enrollment_id: pending_enrollment.id,
enrollment_code: pending_enrollment.enrollment_code,
- exception_class: exception_class,
- exception_message: exception_message,
+ **event_attributes,
)
end
@@ -516,13 +512,14 @@ RSpec.describe GetUspsProofingResultsJob do
context 'when USPS returns a 5xx status code' do
before(:each) do
- stub_request_proofing_results_with_responses({ status: 500 })
+ stub_request_proofing_results_internal_failure
end
it_behaves_like(
'enrollment encountering an exception',
exception_class: 'Faraday::ServerError',
exception_message: 'the server responded with status 500',
+ response_message: 'An internal error occurred processing the request',
)
end
diff --git a/spec/support/usps_ipp_helper.rb b/spec/support/usps_ipp_helper.rb
index 22286669e..ea606e577 100644
--- a/spec/support/usps_ipp_helper.rb
+++ b/spec/support/usps_ipp_helper.rb
@@ -25,8 +25,7 @@ module UspsIppHelper
def stub_request_enroll_internal_failure_response
stub_request(:post, %r{/ivs-ippaas-api/IPPRest/resources/rest/optInIPPApplicant}).to_return(
- status: 500, body: UspsInPersonProofing::Mock::Fixtures.
- request_enroll_internal_failure_response
+ status: 500, body: UspsInPersonProofing::Mock::Fixtures.request_internal_failure_response,
)
end
@@ -42,6 +41,12 @@ module UspsIppHelper
)
end
+ def stub_request_proofing_results_internal_failure
+ stub_request(:post, %r{/ivs-ippaas-api/IPPRest/resources/rest/getProofingResults}).to_return(
+ status: 500, body: UspsInPersonProofing::Mock::Fixtures.request_internal_failure_response,
+ )
+ end
+
def request_expired_proofing_results_args
{
status: 400, body: UspsInPersonProofing::Mock::Fixtures.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I think there's some confusion stemming from our Faraday middlewares which, for any <400 status code, will automatically parse the response body and return it as a hash. For any >=400 status code Faraday will raise an error and we'll have to explicitly access the body buried in the response (eg err.response[:body]) For example this is the response for a 2xx response status code: This is the err.response for a 4xx status code: I also noticed that there's a third type of behavior to account for. If we get a successful response (eg status code 2xx) but the JSON is invalid then Faraday will raise a Faraday::ParsingError. We can look at err.response to see the response object but its body will be nil. If we wanted to access the body (as unparsed text) in that case we could look at
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to create a new ticket to address auditing the different error cases and updating the code to make sure that it behaves as expected for the different types of responses we'll get back from Faraday for various error conditions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ticket to address this thread: https://cm-jira.usa.gov/browse/LG-7706 |
||||||
| else | ||||||
| response_analytics_attributes(nil) | ||||||
| end | ||||||
|
|
||||||
| enrollment_outcomes[:enrollments_errored] += 1 | ||||||
| analytics(user: enrollment.user).idv_in_person_usps_proofing_results_job_exception( | ||||||
| **enrollment_analytics_attributes(enrollment, complete: false), | ||||||
| **response_analytics_attributes(err.response), | ||||||
| **response_attributes, | ||||||
| reason: 'Request exception', | ||||||
| exception_class: err.class.to_s, | ||||||
| exception_message: err.message, | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These errors are unexpected, so I'm explicitly passing them to NewRelic so we can investigate them.