From 332acb75535e1b83ca95b7702963a8bf8bba44bc Mon Sep 17 00:00:00 2001 From: jachamp <28732543+jimchamp@users.noreply.github.com> Date: Thu, 5 Dec 2024 10:55:57 -0800 Subject: [PATCH] Fail gracefully on account creation timeout --- openlibrary/accounts/model.py | 29 ++++++++++++++++--------- openlibrary/plugins/upstream/account.py | 3 +++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/openlibrary/accounts/model.py b/openlibrary/accounts/model.py index 08e52954b50..eff66fe46e1 100644 --- a/openlibrary/accounts/model.py +++ b/openlibrary/accounts/model.py @@ -665,16 +665,22 @@ def create( _screenname = screenname attempt = 0 while True: - response = cls.xauth( - 'create', - email=email, - password=password, - screenname=_screenname, - notifications=notifications, - test=test, - verified=verified, - service='openlibrary', - ) + try: + response = cls.xauth( + 'create', + email=email, + password=password, + screenname=_screenname, + notifications=notifications, + test=test, + verified=verified, + service='openlibrary', + ) + except requests.HTTPError as e: + status_code = e.response.status_code + if status_code == 504: + raise OLAuthenticationError("request_timeout") + raise OLAuthenticationError("undefined_error") if response.get('success'): ia_account = cls.get(email=email) @@ -723,6 +729,9 @@ def xauth(cls, op, test=None, s3_key=None, s3_secret=None, xauth_url=None, **dat params['developer'] = test response = requests.post(url, params=params, json=data) + if response.status_code == 504 and op == "create": + response.raise_for_status() + try: # This API should always return json, even on error (Unless # the server is down or something :P) diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py index d6eccd18643..10e29c188e8 100644 --- a/openlibrary/plugins/upstream/account.py +++ b/openlibrary/plugins/upstream/account.py @@ -89,6 +89,9 @@ def get_login_error(error_key): "invalid_s3keys": _( 'Login attempted with invalid Internet Archive s3 credentials.' ), + "request_timeout": _( + "Servers are experiencing unusually high traffic, please try again later or email openlibrary@archive.org for help." + ), "undefined_error": _('A problem occurred and we were unable to log you in'), } return LOGIN_ERRORS[error_key]