Skip to content

Handle Twilio errors more gracefully#2308

Merged
monfresh merged 1 commit intomasterfrom
mb-fix-intl-sms
Jul 7, 2018
Merged

Handle Twilio errors more gracefully#2308
monfresh merged 1 commit intomasterfrom
mb-fix-intl-sms

Conversation

@monfresh
Copy link
Contributor

@monfresh monfresh commented Jul 6, 2018

Why: So users don't get 500 errors when signing in.

This is a follow up to the previous commit which adds the following:

  • Rescue Twilio errors when signing in and display the OTP verification
    page with an error message, allowing the user to try the phone call
    option, or signing in with their personal key

  • Capture the country code and context along with Twilio errors so we
    can more easily see which countries are affected

  • Don't change the user's OTP delivery preference if the last preference
    they used resulted in a Twilio error. For example, if a user normally
    uses voice, but for some reason didn't get the phone call, and then
    tries to send an SMS to a landline, resulting in an error message, we
    should leave their preference as voice so they won't get an error the
    next time they sign in.

  • Add the phone number country to the Exception Notification emails

Hi! Before submitting your PR for review, and/or before merging it, please
go through the following checklist:

  • For DB changes, check for missing indexes, check to see if the changes
    affect other apps (such as the dashboard), make sure the DB columns in the
    various environments are properly populated, coordinate with devops, plan
    migrations in separate steps.

  • For route changes, make sure GET requests don't change state or result in
    destructive behavior. GET requests should only result in information being
    read, not written.

  • For encryption changes, make sure it is compatible with data that was
    encrypted with the old code.

  • For secrets changes, make sure to update the S3 secrets bucket with the
    new configs in all environments.

  • Do not disable Rubocop or Reek offenses unless you are absolutely sure
    they are false positives. If you're not sure how to fix the offense, please
    ask a teammate.

  • When reading data, write tests for nil values, empty strings,
    and invalid formats.

  • When calling redirect_to in a controller, use _url, not _path.

  • When adding user data to the session, use the user_session helper
    instead of the session helper so the data does not persist beyond the user's
    session.

  • When adding a new controller that requires the user to be fully
    authenticated, make sure to add before_action :confirm_two_factor_authenticated.

**Why**: So users don't get 500 errors when signing in.

This is a follow up to the previous commit which adds the following:

- Rescue Twilio errors when signing in and display the OTP verification
page with an error message, allowing the user to try the phone call
option, or signing in with their personal key

- Capture the country code and context along with Twilio errors so we
can more easily see which countries are affected

- Don't change the user's OTP delivery preference if the last preference
they used resulted in a Twilio error. For example, if a user normally
uses voice, but for some reason didn't get the phone call, and then
tries to send an SMS to a landline, resulting in an error message, we
should leave their preference as voice so they won't get an error the
next time they sign in.

- Add the phone number country to the Exception Notification emails
@monfresh monfresh requested a review from jmhooper July 6, 2018 16:01
end
end

def update_otp_delivery_preference_if_needed
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes a lot more sense here

redirect_back(fallback_location: account_url)
end

# rubocop:disable Metrics/MethodLength
Copy link
Contributor

Choose a reason for hiding this comment

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

This method is a bit long. What would you think about moving the top part into a base_analytics_hash_for_exception method or something like that?

Copy link
Contributor Author

@monfresh monfresh Jul 6, 2018

Choose a reason for hiding this comment

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

yeah, I can do that. Ideally, we'd get rid of the background jobs for voice/sms and use a Form Object instead, and we can move all this logic inside there, and avoid using exceptions for control flow.

@monfresh
Copy link
Contributor Author

monfresh commented Jul 7, 2018

Knowing that we are planning on refactoring this, I'm going to merge this as is, just so we have something we can deploy if necessary. As of earlier this morning, Twilio has disabled their Lookup feature for us, so users are no longer seeing 500 errors. They are investigating why numbers that were able to receive SMS in the past are now being treated as landlines that can't receive SMS.

@monfresh monfresh merged commit a99f153 into master Jul 7, 2018
@monfresh monfresh deleted the mb-fix-intl-sms branch July 7, 2018 00:02
monfresh added a commit that referenced this pull request Jul 9, 2018
**Why**: This TODO that had been previously removed got added back by
mistake in #2308 and I forgot to remove it before merging the PR.
@monfresh monfresh mentioned this pull request Jul 9, 2018
9 tasks
User UUID: <%= @kontroller.analytics_user.uuid %>
<% user = @kontroller.analytics_user %>
User UUID: <%= user.uuid %>
User's Country (based on phone): <%= Phonelib.parse(user.phone).country %>
Copy link
Contributor

Choose a reason for hiding this comment

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

@kontroller.analytics_user may be nil, so in some circumstances the exception notifier itself is exploding here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If there is a controller, analytics_user cannot be nil. If there is no current_user, it is an instance of AnonymousUser. Perhaps what is happening is that @kontroller can be nil in some cases. Do you have a specific scenario?

Copy link
Contributor

Choose a reason for hiding this comment

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

There won't be a controller if rails dies before serving a request. Encountered this in my environment.

Copy link
Contributor

Choose a reason for hiding this comment

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

  ERROR: Failed to generate exception summary:

  ActionView::Template::Error: undefined method `uuid' for nil:NilClass

  /srv/idp/releases/chef/app/views/exception_notifier/_session.text.erb:19:in `_app_views_exception_notifier__session_text_erb__269420484499780557_47443798649360'

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually that message pretty strongly points to analytics_user being nil when the controller is present.

Copy link
Contributor

Choose a reason for hiding this comment

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

In this case triggered by a redis connection failure.

A Redis::CannotConnectError occurred in #:

  Error connecting to Redis on redis.login.gov.internal:6379 (Errno::ECONNREFUSED)
  lib/utf8_sanitizer.rb:20:in `call'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see it now in the logs. Looks like it's happening before it gets to the controller. I will fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 Thanks, no rush

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants