Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/controllers/idv/in_person/usps_locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,19 @@ def index
response = proofer.request_pilot_facilities
end
render json: response.to_json
rescue Faraday::TimeoutError, Faraday::BadRequestError, Faraday::ForbiddenError => err
analytics.idv_in_person_locations_request_failure(
api_status_code: 422,
exception_class: err.class,
exception_message: err.message,
response_body_present: err.respond_to?(:response_body) && err.response_body.present?,
response_body: err.respond_to?(:response_body) && err.response_body,
response_status_code: err.respond_to?(:response_status) && err.response_status,
)
Comment on lines +30 to +37
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this was an urgent change but I wonder if we can leverage something inside of Analytics class that pulls out relevant info from what looks like a pretty standard interface for this err variable.

I'm hoping for some "decorator" style patterns (or something) to help prevent bloat in these controller methods. Most of the code here is error handling :)

Happy to open a ticket/refactor later if that makes sense

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@allthesignals I like that idea -- will you create a ticket for it? We do some similar things in the proofing results job and I wonder if there's a better/more general way to do it, but a way that prevents PII from being logged

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rescue_from looks interesting.

@allthesignals I like that idea -- will you create a ticket for it? We do some similar things in the proofing results job and I wonder if there's a better/more general way to do it, but a way that prevents PII from being logged

Sure thing!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that's a great idea. Thanks @allthesignals! 🎉

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

render json: {}, status: :unprocessable_entity
rescue => err
analytics.idv_in_person_locations_request_failure(
api_status_code: 500,
exception_class: err.class,
exception_message: err.message,
response_body_present: err.respond_to?(:response_body) && err.response_body.present?,
Expand Down
29 changes: 28 additions & 1 deletion spec/controllers/idv/in_person/usps_locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@
allow(proofer).to receive(:request_facilities).with(address).and_raise(timeout_error)
end

it 'returns an internal server error' do
it 'returns an unprocessible entity client error' do
subject
expect(@analytics).to have_logged_event(
'Request USPS IPP locations: request failed',
api_status_code: 422,
exception_class: timeout_error.class,
exception_message: timeout_error.message,
response_body_present:
Expand All @@ -152,6 +153,31 @@
response_status_code: timeout_error.response_status,
)

status = response.status
expect(status).to eq 422
end
end

context 'with a 500 error from USPS' do
let(:server_error) { Faraday::ServerError.new }

before do
allow(proofer).to receive(:request_facilities).with(address).and_raise(server_error)
end

it 'returns an internal server error' do
subject
expect(@analytics).to have_logged_event(
'Request USPS IPP locations: request failed',
api_status_code: 500,
exception_class: server_error.class,
exception_message: server_error.message,
response_body_present:
server_error.response_body.present?,
response_body: server_error.response_body,
response_status_code: server_error.response_status,
)

status = response.status
expect(status).to eq 500
end
Expand All @@ -175,6 +201,7 @@
subject
expect(@analytics).to have_logged_event(
'Request USPS IPP locations: request failed',
api_status_code: 500,
exception_class: exception.class,
exception_message: exception.message,
response_body_present:
Expand Down