Skip to content

Commit

Permalink
chore: Raise HTTP 501 instead of raising NotImplementedError, to prov…
Browse files Browse the repository at this point in the history
…ide more specific error message
  • Loading branch information
jpmckinney committed Aug 23, 2024
1 parent 885b7c8 commit 766b7c6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
7 changes: 6 additions & 1 deletion app/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
from typing import Callable

import boto3
from fastapi import HTTPException, status
from mypy_boto3_cognito_idp import CognitoIdentityProviderClient, literals, type_defs
from mypy_boto3_ses.client import SESClient

from app.i18n import _
from app.settings import app_settings

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -170,7 +172,10 @@ def respond_to_auth_challenge(
"refresh_token": challenge_response["AuthenticationResult"]["RefreshToken"],
}
case _:
raise NotImplementedError
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail=_("Authentication challenge not implemented"),
)


ses_client = boto3.client(
Expand Down
5 changes: 4 additions & 1 deletion app/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ def raise_if_unauthorized(
if user.lender_id == application.lender_id:
break
case _:
raise NotImplementedError
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail=_("Authorization group not implemented"),
)
else:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
Expand Down
5 changes: 4 additions & 1 deletion app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ def login(
mfa_code=payload.temp_password,
)
else:
raise NotImplementedError # Missing MFA challenge
raise HTTPException(
status_code=status.HTTP_501_NOT_IMPLEMENTED,
detail=_("Missing MFA challenge"),
)
# The user failed to sign in ("Incorrect username or password", etc.).
except client.cognito.exceptions.NotAuthorizedException:
raise HTTPException(
Expand Down
5 changes: 3 additions & 2 deletions docs/contributing/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ API design

- ``status.HTTP_404_NOT_FOUND``, if the resource is not found
- ``status.HTTP_409_CONFLICT``, if the resource already exists
- ``status.HTTP_422_UNPROCESSABLE_ENTITY``, otherwise
- ``status.HTTP_501_NOT_IMPLEMENTED``, if the code path is not implemented
- ``status.HTTP_422_UNPROCESSABLE_ENTITY`` for problems with the request, otherwise

.. seealso::

- `422 Unprocessable Content <https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422>`__ (MDN)
- Choosing an HTTP Status Code <https://www.codetinkerer.com/2015/12/04/choosing-an-http-status-code.html>`__
- `Choosing an HTTP Status Code <https://www.codetinkerer.com/2015/12/04/choosing-an-http-status-code.html>`__
34 changes: 23 additions & 11 deletions locale/es/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2024-08-23 13:55-0400\n"
"POT-Creation-Date: 2024-08-23 14:39-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: es\n"
Expand All @@ -35,6 +35,10 @@ msgstr "JWK inválido"
msgid "Not authenticated"
msgstr "No autenticado"

#: app/aws.py:177
msgid "Authentication challenge not implemented"
msgstr "Desafío de autenticación no implementado"

#: app/dependencies.py:44
msgid "Username missing"
msgstr "Falta el nombre de usuario"
Expand All @@ -47,24 +51,28 @@ msgstr "Usuario no encontrado"
msgid "Insufficient permissions"
msgstr "Permisos insuficientes"

#: app/dependencies.py:98
#: app/dependencies.py:96
msgid "Authorization group not implemented"
msgstr "Grupo de autorización no implementado"

#: app/dependencies.py:101
msgid "User is not authorized"
msgstr "Usuario no autorizado"

#: app/dependencies.py:106
#: app/dependencies.py:109
msgid "Application expired"
msgstr "La aplicación ha expirado"

#: app/dependencies.py:113
#: app/dependencies.py:116
#, python-format
msgid "Application status should not be %(status)s"
msgstr "El estado de la aplicación no debe ser %(status)s"

#: app/dependencies.py:126 app/dependencies.py:166
#: app/dependencies.py:129 app/dependencies.py:169
msgid "Application not found"
msgstr "La aplicación no ha sido encontrada"

#: app/dependencies.py:172
#: app/dependencies.py:175
msgid "Application lapsed"
msgstr "La aplicación ha caducado"

Expand Down Expand Up @@ -401,7 +409,7 @@ msgstr "Ya existe un entidad financiera con ese nombre"
msgid "Credit product not found"
msgstr "Producto crediticio no encontrado"

#: app/routers/users.py:56 app/routers/users.py:342
#: app/routers/users.py:56 app/routers/users.py:345
msgid "User with that email already exists"
msgstr "Ya existe un usuario con ese correo"

Expand All @@ -417,23 +425,27 @@ msgstr "Contraseña cambiada"
msgid "Invalid session for the user, session is expired"
msgstr "Sesión inválida para el usuario, la sesión ha expirado"

#: app/routers/users.py:131 app/routers/users.py:184
#: app/routers/users.py:131 app/routers/users.py:187
msgid "Invalid MFA code"
msgstr "Código MFA no válido"

#: app/routers/users.py:134
msgid "MFA configured successfully"
msgstr "El MFA fue configurado correctamente"

#: app/routers/users.py:160 app/routers/users.py:179
#: app/routers/users.py:160 app/routers/users.py:182
msgid "Invalid username or password"
msgstr "Nombre de usuario o contraseña no válidos"

#: app/routers/users.py:215
#: app/routers/users.py:176
msgid "Missing MFA challenge"
msgstr "Falta el desafío de MFA"

#: app/routers/users.py:218
msgid "User logged out successfully"
msgstr "Usuario deslogueado correctamente"

#: app/routers/users.py:261
#: app/routers/users.py:264
msgid "An email with a reset link was sent to end user"
msgstr "Se envió un correo con el enlace de reseteo de contraseña."

Expand Down

0 comments on commit 766b7c6

Please sign in to comment.