Skip to content

Commit

Permalink
Add error message above field
Browse files Browse the repository at this point in the history
  • Loading branch information
berrydenhartog committed Oct 23, 2024
1 parent dba3cf7 commit e7e2f9c
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 52 deletions.
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
38 changes: 23 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 @@ -415,7 +423,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 +609,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 +638,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.
38 changes: 23 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 @@ -416,7 +424,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 +610,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 +639,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.
42 changes: 25 additions & 17 deletions amt/locale/nl_NL/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ msgstr "Data"
msgid "Model"
msgstr "Model"

#: 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 "Instrumenten"

Expand Down Expand Up @@ -162,6 +162,14 @@ msgstr "Verboden AI"
msgid "Exception of application"
msgstr "Uitzondering van toepassing"

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

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

#: amt/core/exceptions.py:20
msgid ""
"An error occurred while configuring the options for '{field}'. Please "
Expand Down Expand Up @@ -434,7 +442,7 @@ msgstr "Algoritmesysteem naam"

#: 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 "Levenscyclus"

Expand Down Expand Up @@ -620,31 +628,31 @@ msgstr "Creëer een Algoritmesysteem"

#: amt/site/templates/projects/new.html.j2:23
msgid "Algorithm System name"
msgstr "Naam algoritmesysteem"
msgstr "Algoritme Systeem naam"

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

#: 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 ""
"Selecteer de levenscyclus waarin uw algoritmesysteem zich momenteel "
"bevindt."

#: 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 "Lees voor meer meer informatie over levenscyclus het"

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

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

#: 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 @@ -656,35 +664,35 @@ msgstr ""
"onder andere het type AI systeem, de regelgeving die van toepassing is en"
" de risicocategorie."

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

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

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

#: 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 ""
"Overzicht van aanbevolen instrument voor het verantwoord ontwikkelen, "
"gebruiken, beoordelen en monitoren van algoritmes en AI-systemen."

#: amt/site/templates/projects/new.html.j2:155
#: amt/site/templates/projects/new.html.j2:157
msgid "Choose one or more instruments"
msgstr "Kies één of meerdere instrumenten"

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

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

5 changes: 5 additions & 0 deletions amt/site/static/scss/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,8 @@ main {
.rvo-table-row td:first-child {
width: 20%;
}

.amt-error-message {
color: var(--rvo-form-feedback-error-color);
font-weight: var(--rvo-form-feedback-error-font-weight);
}
Loading

0 comments on commit e7e2f9c

Please sign in to comment.