Skip to content

Commit 8ac1c97

Browse files
committed
🐛 Refactor error handling to replace 'reason' with 'text' for consistency in HTTP responses
1 parent 9ef6109 commit 8ac1c97

File tree

24 files changed

+57
-69
lines changed

24 files changed

+57
-69
lines changed

packages/service-library/src/servicelib/aiohttp/profiler_middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async def profiling_middleware(request: Request, handler):
1313
try:
1414
if _profiler.is_running or (_profiler.last_session is not None):
1515
raise HTTPInternalServerError(
16-
reason="Profiler is already running. Only a single request can be profiled at any given time.",
16+
text="Profiler is already running. Only a single request can be profiled at any given time.",
1717
headers={},
1818
)
1919
_profiler.reset()
@@ -24,7 +24,7 @@ async def profiling_middleware(request: Request, handler):
2424

2525
if response.content_type != MIMETYPE_APPLICATION_JSON:
2626
raise HTTPInternalServerError(
27-
reason=f"Profiling middleware is not compatible with {response.content_type=}",
27+
text=f"Profiling middleware is not compatible with {response.content_type=}",
2828
headers={},
2929
)
3030

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ async def check_authorized_user_credentials_or_raise(
5353

5454
if not user:
5555
raise web.HTTPUnauthorized(
56-
reason=MSG_UNKNOWN_EMAIL, content_type=MIMETYPE_APPLICATION_JSON
56+
text=MSG_UNKNOWN_EMAIL, content_type=MIMETYPE_APPLICATION_JSON
5757
)
5858

5959
_login_service.validate_user_status(user=user, support_email=product.support_email)
6060

6161
if not security_service.check_password(password, user["password_hash"]):
6262
raise web.HTTPUnauthorized(
63-
reason=MSG_WRONG_PASSWORD, content_type=MIMETYPE_APPLICATION_JSON
63+
text=MSG_WRONG_PASSWORD, content_type=MIMETYPE_APPLICATION_JSON
6464
)
6565
return user
6666

@@ -83,5 +83,5 @@ async def check_authorized_user_in_product_or_raise(
8383
)
8484
):
8585
raise web.HTTPUnauthorized(
86-
reason=MSG_UNKNOWN_EMAIL, content_type=MIMETYPE_APPLICATION_JSON
86+
text=MSG_UNKNOWN_EMAIL, content_type=MIMETYPE_APPLICATION_JSON
8787
)

services/web/server/src/simcore_service_webserver/login/_controller/rest/change.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ async def change_password(request: web.Request):
297297
passwords.current.get_secret_value(), user["password_hash"]
298298
):
299299
raise web.HTTPUnprocessableEntity(
300-
reason=MSG_WRONG_PASSWORD, content_type=MIMETYPE_APPLICATION_JSON
300+
text=MSG_WRONG_PASSWORD, content_type=MIMETYPE_APPLICATION_JSON
301301
) # 422
302302

303303
await db.update_user(

services/web/server/src/simcore_service_webserver/login/_controller/rest/confirmation.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ async def phone_confirmation(request: web.Request):
237237

238238
if not settings.LOGIN_2FA_REQUIRED:
239239
raise web.HTTPServiceUnavailable(
240-
reason="Phone registration is not available",
240+
text="Phone registration is not available",
241241
content_type=MIMETYPE_APPLICATION_JSON,
242242
)
243243

@@ -257,15 +257,15 @@ async def phone_confirmation(request: web.Request):
257257

258258
except UniqueViolation as err:
259259
raise web.HTTPUnauthorized(
260-
reason="Invalid phone number",
260+
text="Invalid phone number",
261261
content_type=MIMETYPE_APPLICATION_JSON,
262262
) from err
263263

264264
return await _security_service.login_granted_response(request, user=dict(user))
265265

266266
# fails because of invalid or no code
267267
raise web.HTTPUnauthorized(
268-
reason="Invalid 2FA code", content_type=MIMETYPE_APPLICATION_JSON
268+
text="Invalid 2FA code", content_type=MIMETYPE_APPLICATION_JSON
269269
)
270270

271271

@@ -312,7 +312,7 @@ async def complete_reset_password(request: web.Request):
312312
return flash_response(MSG_PASSWORD_CHANGED)
313313

314314
raise web.HTTPUnauthorized(
315-
reason=MSG_PASSWORD_CHANGE_NOT_ALLOWED.format(
315+
text=MSG_PASSWORD_CHANGE_NOT_ALLOWED.format(
316316
support_email=product.support_email
317317
),
318318
content_type=MIMETYPE_APPLICATION_JSON,

services/web/server/src/simcore_service_webserver/login/_controller/rest/preregistration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async def request_product_account(request: web.Request):
7272

7373
if body.captcha != session.get(CAPTCHA_SESSION_KEY):
7474
raise web.HTTPUnprocessableEntity(
75-
reason=MSG_WRONG_CAPTCHA__INVALID, content_type=MIMETYPE_APPLICATION_JSON
75+
text=MSG_WRONG_CAPTCHA__INVALID, content_type=MIMETYPE_APPLICATION_JSON
7676
)
7777
session.pop(CAPTCHA_SESSION_KEY, None)
7878

@@ -113,7 +113,7 @@ async def unregister_account(request: web.Request):
113113
body.password.get_secret_value(), credentials.password_hash
114114
):
115115
raise web.HTTPConflict(
116-
reason="Wrong email or password. Please try again to delete this account"
116+
text="Wrong email or password. Please try again to delete this account"
117117
)
118118

119119
with log_context(

services/web/server/src/simcore_service_webserver/login/_controller/rest/registration.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ async def register(request: web.Request):
168168
< settings.LOGIN_PASSWORD_MIN_LENGTH
169169
):
170170
raise web.HTTPUnauthorized(
171-
reason=MSG_WEAK_PASSWORD.format(
171+
text=MSG_WEAK_PASSWORD.format(
172172
LOGIN_PASSWORD_MIN_LENGTH=settings.LOGIN_PASSWORD_MIN_LENGTH
173173
),
174174
content_type=MIMETYPE_APPLICATION_JSON,
@@ -198,7 +198,7 @@ async def register(request: web.Request):
198198
invitation_code = registration.invitation
199199
if invitation_code is None:
200200
raise web.HTTPBadRequest(
201-
reason="invitation field is required",
201+
text="invitation field is required",
202202
content_type=MIMETYPE_APPLICATION_JSON,
203203
)
204204

@@ -374,7 +374,7 @@ async def register_phone(request: web.Request):
374374

375375
if not settings.LOGIN_2FA_REQUIRED:
376376
raise web.HTTPServiceUnavailable(
377-
reason="Phone registration is not available",
377+
text="Phone registration is not available",
378378
content_type=MIMETYPE_APPLICATION_JSON,
379379
)
380380

@@ -436,6 +436,6 @@ async def register_phone(request: web.Request):
436436
)
437437

438438
raise web.HTTPServiceUnavailable(
439-
reason=user_error_msg,
439+
text=user_error_msg,
440440
content_type=MIMETYPE_APPLICATION_JSON,
441441
) from err

services/web/server/src/simcore_service_webserver/login/_controller/rest/twofa.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ async def resend_2fa_code(request: web.Request):
5555
user = await db.get_user({"email": resend_2fa_.email})
5656
if not user:
5757
raise web.HTTPUnauthorized(
58-
reason=MSG_UNKNOWN_EMAIL, content_type=MIMETYPE_APPLICATION_JSON
58+
text=MSG_UNKNOWN_EMAIL, content_type=MIMETYPE_APPLICATION_JSON
5959
)
6060

6161
if not settings.LOGIN_2FA_REQUIRED:
6262
raise web.HTTPServiceUnavailable(
63-
reason="2FA login is not available",
63+
text="2FA login is not available",
6464
content_type=MIMETYPE_APPLICATION_JSON,
6565
)
6666

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async def _raise_if_registered_in_product(app: web.Application, user_email, prod
102102
app, user_email=user_email, group_id=product.group_id
103103
):
104104
raise web.HTTPConflict(
105-
reason=MSG_EMAIL_ALREADY_REGISTERED,
105+
text=MSG_EMAIL_ALREADY_REGISTERED,
106106
content_type=MIMETYPE_APPLICATION_JSON,
107107
)
108108

@@ -163,7 +163,7 @@ async def check_other_registrations(
163163
UserStatus.DELETED,
164164
)
165165
raise web.HTTPConflict(
166-
reason=MSG_USER_DISABLED.format(
166+
text=MSG_USER_DISABLED.format(
167167
support_email=current_product.support_email
168168
),
169169
content_type=MIMETYPE_APPLICATION_JSON,
@@ -230,7 +230,7 @@ def _invitations_request_context(invitation_code: str) -> Iterator[URL]:
230230
)
231231
)
232232
raise web.HTTPForbidden(
233-
reason=user_error_msg,
233+
text=user_error_msg,
234234
content_type=MIMETYPE_APPLICATION_JSON,
235235
) from err
236236

@@ -247,7 +247,7 @@ def _invitations_request_context(invitation_code: str) -> Iterator[URL]:
247247
)
248248
)
249249
raise web.HTTPServiceUnavailable(
250-
reason=user_error_msg,
250+
text=user_error_msg,
251251
content_type=MIMETYPE_APPLICATION_JSON,
252252
) from err
253253

@@ -323,7 +323,7 @@ async def check_and_consume_invitation(
323323
_logger.info("Invitation with %s was consumed", f"{confirmation_token=}")
324324

325325
raise web.HTTPForbidden(
326-
reason=(
326+
text=(
327327
"Invalid invitation code."
328328
"Your invitation was already used or might have expired."
329329
+ MSG_INVITATIONS_CONTACT_SUFFIX

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,25 @@ def validate_user_status(*, user: dict, support_email: str):
5656

5757
if user_status == DELETED:
5858
raise web.HTTPUnauthorized(
59-
reason=MSG_USER_DELETED.format(support_email=support_email),
59+
text=MSG_USER_DELETED.format(support_email=support_email),
6060
content_type=MIMETYPE_APPLICATION_JSON,
6161
) # 401
6262

6363
if user_status == BANNED or user["role"] == ANONYMOUS:
6464
raise web.HTTPUnauthorized(
65-
reason=MSG_USER_BANNED.format(support_email=support_email),
65+
text=MSG_USER_BANNED.format(support_email=support_email),
6666
content_type=MIMETYPE_APPLICATION_JSON,
6767
) # 401
6868

6969
if user_status == EXPIRED:
7070
raise web.HTTPUnauthorized(
71-
reason=MSG_USER_EXPIRED.format(support_email=support_email),
71+
text=MSG_USER_EXPIRED.format(support_email=support_email),
7272
content_type=MIMETYPE_APPLICATION_JSON,
7373
) # 401
7474

7575
if user_status == CONFIRMATION_PENDING:
7676
raise web.HTTPUnauthorized(
77-
reason=MSG_ACTIVATION_REQUIRED,
77+
text=MSG_ACTIVATION_REQUIRED,
7878
content_type=MIMETYPE_APPLICATION_JSON,
7979
) # 401
8080

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async def wrapper(request: web.Request) -> web.StreamResponse:
4141
)
4242
)
4343
raise web.HTTPServiceUnavailable(
44-
reason=front_end_msg,
44+
text=front_end_msg,
4545
content_type=MIMETYPE_APPLICATION_JSON,
4646
) from exc
4747

0 commit comments

Comments
 (0)