Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error message above field #302

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions amt/core/exception_handlers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import logging
from gettext import gettext as _
from typing import Any

from babel.support import NullTranslations
from fastapi import Request, status
from fastapi.exceptions import RequestValidationError
from fastapi.responses import HTMLResponse
Expand All @@ -13,6 +16,21 @@

logger = logging.getLogger(__name__)

CUSTOM_MESSAGES = {
"string_too_short": _("String should have at least {min_length} characters"),
"missing": _("Field required"),
}


def translate_pydantic_exception(err: dict[str, Any], translations: NullTranslations) -> str:
message: str | None = CUSTOM_MESSAGES.get(err["type"], None)

if message:
custom_message = translations.gettext(message)
return custom_message.format(**err["ctx"]) if "ctx" in err else custom_message

return err["msg"]


async def general_exception_handler(request: Request, exc: Exception) -> HTMLResponse:
exception_name = exc.__class__.__name__
Expand All @@ -27,8 +45,10 @@ async def general_exception_handler(request: Request, exc: Exception) -> HTMLRes
elif isinstance(exc, StarletteHTTPException):
message = AMTNotFound().getmessage(translations) if exc.status_code == status.HTTP_404_NOT_FOUND else exc.detail
elif isinstance(exc, RequestValidationError):
messages: list[str] = [f"{error['loc'][-1]}: {error['msg']}" for error in exc.errors()]
message = "\n".join(messages)
# i assume only pydantic errors get into this section
message = exc.errors()
for err in message:
err["msg"] = translate_pydantic_exception(err, translations)

status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
if isinstance(exc, StarletteHTTPException):
Expand All @@ -42,6 +62,7 @@ async def general_exception_handler(request: Request, exc: Exception) -> HTMLRes
if request.state.htmx
else f"errors/{exception_name}_{status_code}.html.j2"
)

fallback_template_name = "errors/_Exception.html.j2" if request.state.htmx else "errors/Exception.html.j2"

response: HTMLResponse | None = None
Expand Down
46 changes: 31 additions & 15 deletions amt/locale/base.pot
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ msgstr ""
msgid "Model"
msgstr ""

#: amt/api/navigation.py:59 amt/site/templates/projects/new.html.j2:145
#: amt/api/navigation.py:59 amt/site/templates/projects/new.html.j2:147
msgid "Instruments"
msgstr ""

Expand Down Expand Up @@ -161,6 +161,14 @@ msgstr ""
msgid "Exception of application"
msgstr ""

#: amt/core/exception_handlers.py:20
msgid "String should have at least {min_length} characters"
msgstr ""

#: amt/core/exception_handlers.py:21
msgid "Field required"
msgstr ""

#: amt/core/exceptions.py:20
msgid ""
"An error occurred while configuring the options for '{field}'. Please "
Expand Down Expand Up @@ -263,6 +271,14 @@ msgstr ""
msgid "An error occurred. Please try again later"
msgstr ""

#: amt/site/templates/errors/_RequestValidationError_400.html.j2:3
msgid "There are some errors"
msgstr ""

#: amt/site/templates/errors/_RequestValidationError_400.html.j2:5
msgid "There is one error:"
msgstr ""

#: amt/site/templates/layouts/base.html.j2:11
msgid "Algorithmic Management Toolkit (AMT)"
msgstr ""
Expand Down Expand Up @@ -415,7 +431,7 @@ msgstr ""

#: amt/site/templates/parts/filter_list.html.j2:99
#: amt/site/templates/projects/details_info.html.j2:24
#: amt/site/templates/projects/new.html.j2:37
#: amt/site/templates/projects/new.html.j2:38
msgid "Lifecycle"
msgstr ""

Expand Down Expand Up @@ -601,27 +617,27 @@ msgstr ""
msgid "Algorithm System name"
msgstr ""

#: amt/site/templates/projects/new.html.j2:26
#: amt/site/templates/projects/new.html.j2:27
msgid "Name of the algorithm system"
msgstr ""

#: amt/site/templates/projects/new.html.j2:39
#: amt/site/templates/projects/new.html.j2:41
msgid "Select the lifecycle your algorithm system is currently in."
msgstr ""

#: amt/site/templates/projects/new.html.j2:40
#: amt/site/templates/projects/new.html.j2:42
msgid "For more information on lifecycle, read the"
msgstr ""

#: amt/site/templates/projects/new.html.j2:43
#: amt/site/templates/projects/new.html.j2:45
msgid "Algorithm Framework"
msgstr ""

#: amt/site/templates/projects/new.html.j2:61
#: amt/site/templates/projects/new.html.j2:63
msgid "AI Act Profile"
msgstr ""

#: amt/site/templates/projects/new.html.j2:63
#: amt/site/templates/projects/new.html.j2:65
msgid ""
"The AI Act profile provides insight into, among other things, the type of"
" AI system and the associated obligations from the European AI Act. If "
Expand All @@ -630,33 +646,33 @@ msgid ""
"tree."
msgstr ""

#: amt/site/templates/projects/new.html.j2:70
#: amt/site/templates/projects/new.html.j2:72
msgid "Find your AI Act profile"
msgstr ""

#: amt/site/templates/projects/new.html.j2:128
#: amt/site/templates/projects/new.html.j2:130
msgid "Yes"
msgstr ""

#: amt/site/templates/projects/new.html.j2:138
#: amt/site/templates/projects/new.html.j2:140
msgid "No"
msgstr ""

#: amt/site/templates/projects/new.html.j2:147
#: amt/site/templates/projects/new.html.j2:149
msgid ""
"Overview of instruments for the responsible development, deployment, "
"assessment and monitoring of algorithms and AI-systems."
msgstr ""

#: amt/site/templates/projects/new.html.j2:155
#: amt/site/templates/projects/new.html.j2:157
msgid "Choose one or more instruments"
msgstr ""

#: amt/site/templates/projects/new.html.j2:177
#: amt/site/templates/projects/new.html.j2:181
msgid "Create Algorithm System"
msgstr ""

#: amt/site/templates/projects/new.html.j2:195
#: amt/site/templates/projects/new.html.j2:198
msgid "Copy results and close"
msgstr ""

Binary file modified amt/locale/en_US/LC_MESSAGES/messages.mo
Binary file not shown.
46 changes: 31 additions & 15 deletions amt/locale/en_US/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ msgstr ""
msgid "Model"
msgstr ""

#: amt/api/navigation.py:59 amt/site/templates/projects/new.html.j2:145
#: amt/api/navigation.py:59 amt/site/templates/projects/new.html.j2:147
msgid "Instruments"
msgstr ""

Expand Down Expand Up @@ -162,6 +162,14 @@ msgstr ""
msgid "Exception of application"
msgstr ""

#: amt/core/exception_handlers.py:20
msgid "String should have at least {min_length} characters"
msgstr ""

#: amt/core/exception_handlers.py:21
msgid "Field required"
msgstr ""

#: amt/core/exceptions.py:20
msgid ""
"An error occurred while configuring the options for '{field}'. Please "
Expand Down Expand Up @@ -264,6 +272,14 @@ msgstr ""
msgid "An error occurred. Please try again later"
msgstr ""

#: amt/site/templates/errors/_RequestValidationError_400.html.j2:3
msgid "There are some errors"
msgstr ""

#: amt/site/templates/errors/_RequestValidationError_400.html.j2:5
msgid "There is one error:"
msgstr ""

#: amt/site/templates/layouts/base.html.j2:11
msgid "Algorithmic Management Toolkit (AMT)"
msgstr ""
Expand Down Expand Up @@ -416,7 +432,7 @@ msgstr ""

#: amt/site/templates/parts/filter_list.html.j2:99
#: amt/site/templates/projects/details_info.html.j2:24
#: amt/site/templates/projects/new.html.j2:37
#: amt/site/templates/projects/new.html.j2:38
msgid "Lifecycle"
msgstr ""

Expand Down Expand Up @@ -602,27 +618,27 @@ msgstr ""
msgid "Algorithm System name"
msgstr ""

#: amt/site/templates/projects/new.html.j2:26
#: amt/site/templates/projects/new.html.j2:27
msgid "Name of the algorithm system"
msgstr ""

#: amt/site/templates/projects/new.html.j2:39
#: amt/site/templates/projects/new.html.j2:41
msgid "Select the lifecycle your algorithm system is currently in."
msgstr ""

#: amt/site/templates/projects/new.html.j2:40
#: amt/site/templates/projects/new.html.j2:42
msgid "For more information on lifecycle, read the"
msgstr ""

#: amt/site/templates/projects/new.html.j2:43
#: amt/site/templates/projects/new.html.j2:45
msgid "Algorithm Framework"
msgstr ""

#: amt/site/templates/projects/new.html.j2:61
#: amt/site/templates/projects/new.html.j2:63
msgid "AI Act Profile"
msgstr ""

#: amt/site/templates/projects/new.html.j2:63
#: amt/site/templates/projects/new.html.j2:65
msgid ""
"The AI Act profile provides insight into, among other things, the type of"
" AI system and the associated obligations from the European AI Act. If "
Expand All @@ -631,33 +647,33 @@ msgid ""
"tree."
msgstr ""

#: amt/site/templates/projects/new.html.j2:70
#: amt/site/templates/projects/new.html.j2:72
msgid "Find your AI Act profile"
msgstr ""

#: amt/site/templates/projects/new.html.j2:128
#: amt/site/templates/projects/new.html.j2:130
msgid "Yes"
msgstr ""

#: amt/site/templates/projects/new.html.j2:138
#: amt/site/templates/projects/new.html.j2:140
msgid "No"
msgstr ""

#: amt/site/templates/projects/new.html.j2:147
#: amt/site/templates/projects/new.html.j2:149
msgid ""
"Overview of instruments for the responsible development, deployment, "
"assessment and monitoring of algorithms and AI-systems."
msgstr ""

#: amt/site/templates/projects/new.html.j2:155
#: amt/site/templates/projects/new.html.j2:157
msgid "Choose one or more instruments"
msgstr ""

#: amt/site/templates/projects/new.html.j2:177
#: amt/site/templates/projects/new.html.j2:181
msgid "Create Algorithm System"
msgstr ""

#: amt/site/templates/projects/new.html.j2:195
#: amt/site/templates/projects/new.html.j2:198
msgid "Copy results and close"
msgstr ""

Binary file modified amt/locale/nl_NL/LC_MESSAGES/messages.mo
Binary file not shown.
Loading
Loading