Skip to content

Commit

Permalink
update exception handlers and translations.
Browse files Browse the repository at this point in the history
  • Loading branch information
diversen7 committed Oct 16, 2024
1 parent bb6e9a3 commit be0f5d9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
39 changes: 32 additions & 7 deletions stadsarkiv_client/core/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,63 @@


async def not_found(request: Request, exc: HTTPException):
context_values = {"title": translate("404 Not Found")}
context_values = {
"title": f"404 {translate('Error. Not Found')}",
"status_code": 404,
}

# No need to log full exception. It's a 404
log.error(f"404 Not Found: {request.url}")
log.warning(f"404 Not Found: {request.url}")
context = await get_context(request, context_values=context_values)
return templates.TemplateResponse(request, "errors/default.html", context, status_code=404)


async def http_status_error(request: Request, exc: HTTPStatusError):

exc_traceback = traceback.format_exc()
title = f"{exc.response.status_code}. {translate('Error. Request Error')}"
context_values = {
"title": title,
"status_code": exc.response.status_code,
"exc": exc,
"exc_traceback": exc_traceback,
}

log.exception(f"{exc.response.status_code} Error: {request.url}")
context = await get_context(request, context_values=context_values)
return templates.TemplateResponse(request, "errors/default.html", context, status_code=exc.response.status_code)


async def server_error(request: Request, exc: Exception):

exc_traceback = traceback.format_exc()
context_values = {
"title": translate("500 Server Error"),
"title": f"500 {translate('Error. Server Error')}",
"status_code": 500,
"exc": exc,
"exc_traceback": exc_traceback,
}

log.exception(f"500 Error: {request.url}", exc_info=exc)
log.exception(f"500 Error: {request.url}")
context = await get_context(request, context_values=context_values)
return templates.TemplateResponse(request, "errors/default.html", context, status_code=500)


async def forbidden_error(request: Request, exc: HTTPException):
context_values = {"title": translate("403 Forbidden Error")}
context_values = {
"title": translate("Error. Forbidden Error"),
"status_code": 403,
}

log.error(f"403 Forbidden: {request.url}", exc_info=exc)
log.exception(f"403 Forbidden: {request.url}")
context = await get_context(request, context_values=context_values)
return templates.TemplateResponse(request, "errors/default.html", context, status_code=403)


async def auth_exception_handler(request: Request, exc: AuthException):
flash.set_message(request, exc.message, type="error")
log.exception(f"403 Forbidden: {request.url}")
log.info(f"AuthException Redirect to: {exc.redirect_url}")
return RedirectResponse(url=exc.redirect_url, status_code=302)


Expand All @@ -66,7 +91,7 @@ async def auth_exception_json_handler(request: Request, exc: AuthExceptionJSON):
403: forbidden_error,
404: not_found,
500: server_error,
HTTPStatusError: server_error,
HTTPStatusError: http_status_error,
AuthException: auth_exception_handler,
AuthExceptionJSON: auth_exception_json_handler,
}
7 changes: 4 additions & 3 deletions stadsarkiv_client/locales/da.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
100 per page: 100 per side
20 per page: 20 per side
403 Forbidden Error: 403 Adgang nægtet
404 Not Found: 404 Siden blev ikke fundet
50 per page: 50 per side
500 Server Error: 500 Server Error
A verify link has been sent to your email. You may verify your account now by clicking the link.: Et
verifikations-link er blevet sendt til din e-mail. Du kan nu verificere din konto
ved at klikke på linket.
Expand Down Expand Up @@ -41,6 +38,10 @@ Email or password is incorrect. Or your user has not been activated.: Email elle
Email or password is not correct.: Email eller password er ikke korrekt.
Enter new password: Indtast nyt password
Entity: Entity
Error. Not Found: Siden eksisterer ikke.
Error. Request Error: Der var et problem med din forespørgsel.
Error. Server Error: Der skete en system fejl.
Error. Forbidden Error: Du har ikke adgang til siden.
Failed to get schemas: Det var ikke muligt at hente skemaer
First page: Første
Forgot your password: Glemt password
Expand Down
3 changes: 3 additions & 0 deletions stadsarkiv_client/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ Email or password is incorrect. Or your user has not been activated.: Email or p
Email or password is not correct.: Email or password is not correct.
Enter new password: Enter new password
Entity: Entity
Error. Not Found: Error. Not Found
Error. Request Error: Error. Request Error
Error. Server Error: Error. Server Error
Failed to get schemas: Failed to get schemas
First page: First page
Forgot your password: Forgot your password
Expand Down

0 comments on commit be0f5d9

Please sign in to comment.