Skip to content

Commit

Permalink
Instrument registration flow (#9096)
Browse files Browse the repository at this point in the history
* Throw custom error on IA account creation failure
* Capture registration errors with Sentry

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Mek <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored May 9, 2024
1 parent 7b82a31 commit 46da379
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
17 changes: 10 additions & 7 deletions openlibrary/accounts/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
logger = logging.getLogger("openlibrary.account.model")


class OLAuthenticationError(Exception):
pass


def append_random_suffix(text, limit=9999):
return f'{text}{random.randint(0, limit)}'

Expand Down Expand Up @@ -643,10 +647,10 @@ def create(
notifications = notifications or []

if cls.get(email=email):
raise ValueError('email_registered')
raise OLAuthenticationError('email_registered')

if not screenname:
raise ValueError('screenname required')
raise OLAuthenticationError('missing_fields')

_screenname = screenname
attempt = 0
Expand All @@ -669,13 +673,12 @@ def create(
return ia_account

elif 'screenname' not in response.get('values', {}):
errors = '_'.join(response.get('values', {}))
raise ValueError(errors)
raise OLAuthenticationError('undefined_error')

elif attempt >= retries:
ve = ValueError('username_registered')
ve.value = _screenname
raise ve
e = OLAuthenticationError('username_registered')
e.value = _screenname
raise e

_screenname = append_random_suffix(screenname)
attempt += 1
Expand Down
4 changes: 4 additions & 0 deletions openlibrary/i18n/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -6430,6 +6430,10 @@ msgstr ""
msgid "Login attempted with invalid Internet Archive s3 credentials."
msgstr ""

#: account.py
msgid "A problem occurred and we were unable to log you in"
msgstr ""

#: account.py
#, python-format
msgid ""
Expand Down
10 changes: 8 additions & 2 deletions openlibrary/plugins/upstream/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
)
from openlibrary.core.observations import Observations
from openlibrary.core.ratings import Ratings
from openlibrary.plugins.openlibrary.sentry import sentry
from openlibrary.core.follows import PubSub

from openlibrary.plugins.recaptcha import recaptcha
from openlibrary.plugins.upstream.mybooks import MyBooksTemplate
from openlibrary.plugins import openlibrary as olib
Expand All @@ -42,6 +44,7 @@
InternetArchiveAccount,
valid_email,
clear_cookies,
OLAuthenticationError,
)
from openlibrary.plugins.upstream import borrow, forms, utils
from openlibrary.utils.dateutil import elapsed_time
Expand Down Expand Up @@ -90,6 +93,7 @@ def get_login_error(error_key):
"invalid_s3keys": _(
'Login attempted with invalid Internet Archive s3 credentials.'
),
"undefined_error": _('A problem occurred and we were unable to log you in'),
}
return LOGIN_ERRORS[error_key]

Expand Down Expand Up @@ -319,8 +323,10 @@ def POST(self):
return render['account/verify'](
username=f.username.value, email=f.email.value
)
except ValueError:
f.note = get_login_error('max_retries_exceeded')
except OLAuthenticationError as e:
f.note = get_login_error(e.__str__())
if sentry.enabled:
sentry.capture_exception(e)

return render['account/create'](f)

Expand Down
5 changes: 5 additions & 0 deletions openlibrary/utils/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ def capture_exception_webpy(self):
scope.add_event_processor(add_web_ctx_to_event)
sentry_sdk.capture_exception()

def capture_exception(self, ex):
with sentry_sdk.push_scope() as scope:
scope.add_event_processor(add_web_ctx_to_event)
sentry_sdk.capture_exception(ex)


@dataclass
class InfogamiRoute:
Expand Down

0 comments on commit 46da379

Please sign in to comment.