Skip to content

Commit

Permalink
refactor errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrespov committed Feb 6, 2024
1 parent 26ec76e commit 7a42ed6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ class InvalidRedirectionParams(PydanticErrorMixin, StudyDispatcherError):
"The link you provided is invalid because it doesn't contain any information related to data or a service."
" Please check the link and make sure it is correct."
)


class GuestUsersLimitError(PydanticErrorMixin, StudyDispatcherError):
msg_template = "Maximum number of guests was reached. Please login with a registered user or try again later"
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
MSG_TOO_MANY_GUESTS,
MSG_UNEXPECTED_ERROR,
)
from ._users import MaxGuestUsersError, create_temporary_guest_user, get_authorized_user
from ._errors import GuestUsersLimitError
from ._users import create_temporary_guest_user, get_authorized_user

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -288,7 +289,7 @@ async def get_redirection_to_study_page(request: web.Request) -> web.Response:
user = await create_temporary_guest_user(request)
is_anonymous_user = True

except MaxGuestUsersError as exc:
except GuestUsersLimitError as exc:
#
# NOTE: Creation of guest users is limited. For that
# reason we respond with 429 and inform the user that temporarily
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from ..users.api import get_user
from ..users.exceptions import UserNotFoundError
from ._constants import MSG_GUESTS_NOT_ALLOWED
from ._errors import GuestUsersLimitError
from .settings import StudiesDispatcherSettings, get_plugin_settings

_logger = logging.getLogger(__name__)
Expand All @@ -60,14 +61,6 @@ async def get_authorized_user(request: web.Request) -> dict:
return {}


class MaxGuestUsersError(RuntimeError):
# NOTE: when lock times out it is because a user cannot
# be create in less that MAX_DELAY_TO_CREATE_USER seconds.
# That shows that the system is really loaded and we rather
# stop creating GUEST users.
...


async def create_temporary_guest_user(request: web.Request):
"""Creates a guest user with a random name and
Expand Down Expand Up @@ -141,6 +134,11 @@ async def create_temporary_guest_user(request: web.Request):
).acquire()

except LockNotOwnedError as err:
# NOTE: when lock times out it is because a user cannot
# be create in less that MAX_DELAY_TO_CREATE_USER seconds.
# That shows that the system is really loaded and we rather
# stop creating GUEST users.

# NOTE: here we cleanup but if any trace is left it will be deleted by gc
if usr.get("id"):

Expand All @@ -155,9 +153,7 @@ async def _cleanup(draft_user):
APP_FIRE_AND_FORGET_TASKS_KEY
],
)

msg = f"Load limit reached. Unable to create a user under {MAX_DELAY_TO_CREATE_USER} sec."
raise MaxGuestUsersError(msg) from err
raise GuestUsersLimitError from err

return user

Expand Down

0 comments on commit 7a42ed6

Please sign in to comment.