Skip to content

Commit 9a723d7

Browse files
committed
raises verification errors and logs them in handler
1 parent 04eba2f commit 9a723d7

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

services/web/server/src/simcore_service_webserver/login/_2fa_api.py

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
import logging
1212

1313
from aiohttp import web
14-
from models_library.error_codes import create_error_code
1514
from models_library.users import UserID
1615
from pydantic import BaseModel, Field
17-
from servicelib.logging_utils import LogExtra, get_log_record_extra, log_decorator
16+
from servicelib.logging_utils import log_decorator
1817
from servicelib.utils_secrets import generate_passcode
1918
from settings_library.twilio import TwilioSettings
2019
from twilio.base.exceptions import TwilioException # type: ignore[import-untyped]
@@ -132,15 +131,11 @@ def _sender():
132131
await asyncio.get_event_loop().run_in_executor(executor=None, func=_sender)
133132

134133
except TwilioException as exc:
135-
error_code = create_error_code(exc)
136-
log_extra: LogExtra = get_log_record_extra(user_id=user_id) or {}
137-
log.exception(
138-
"Failed while setting up 2FA code and sending SMS to %s [%s]",
139-
mask_phone_number(phone_number),
140-
f"{error_code}",
141-
extra={"error_code": error_code, **log_extra},
142-
)
143-
raise SendingVerificationSmsError(reason=exc) from exc
134+
raise SendingVerificationSmsError(
135+
reason=f"Could not send SMS to {mask_phone_number(phone_number)}",
136+
user_id=user_id,
137+
twilio_error=exc,
138+
) from exc
144139

145140

146141
#
@@ -177,16 +172,13 @@ async def send_email_code(
177172
"product": product,
178173
},
179174
)
180-
except TwilioException as exc:
181-
error_code = create_error_code(exc)
182-
log_extra: LogExtra = get_log_record_extra(user_id=user_id) or {}
183-
log.exception(
184-
"Failed while setting up 2FA code and sending Email to %s [%s]",
185-
user_email,
186-
f"{error_code}",
187-
extra={"error_code": error_code, **log_extra},
188-
)
189-
raise SendingVerificationEmailError(reason=exc) from exc
175+
except Exception as exc:
176+
raise SendingVerificationEmailError(
177+
reason=f"Could not send email to {user_email}",
178+
user_id=user_id,
179+
user_email=user_email,
180+
email_error=exc,
181+
) from exc
190182

191183

192184
#

services/web/server/src/simcore_service_webserver/login/errors.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import functools
2+
import logging
23

34
from aiohttp import web
45
from servicelib.aiohttp.typing_extension import Handler
6+
from servicelib.logging_errors import create_troubleshotting_log_kwargs
57
from servicelib.mimetype_constants import MIMETYPE_APPLICATION_JSON
68

79
from ..errors import WebServerBaseError
810
from ._constants import MSG_2FA_UNAVAILABLE_OEC
911

12+
_logger = logging.getLogger(__name__)
13+
1014

1115
class LoginError(WebServerBaseError, ValueError):
1216
...
@@ -27,8 +31,14 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
2731
return await handler(request)
2832

2933
except (SendingVerificationSmsError, SendingVerificationEmailError) as exc:
34+
35+
front_end_msg = MSG_2FA_UNAVAILABLE_OEC.format(error_code=exc.error_code())
36+
# in these cases I want to log the cause
37+
_logger.exception(
38+
**create_troubleshotting_log_kwargs(front_end_msg, exception=exc)
39+
)
3040
raise web.HTTPServiceUnavailable(
31-
reason=MSG_2FA_UNAVAILABLE_OEC.format(error_code=exc.code),
41+
reason=front_end_msg,
3242
content_type=MIMETYPE_APPLICATION_JSON,
3343
) from exc
3444

0 commit comments

Comments
 (0)