error handling

This commit is contained in:
Mateusz Gruszczyński
2026-03-20 10:54:01 +01:00
parent bbfb3e0887
commit 29b07f6431
2 changed files with 118 additions and 52 deletions

View File

@@ -1,4 +1,3 @@
from html import escape
from http import HTTPStatus
from flask import jsonify, render_template, request
@@ -6,20 +5,21 @@ from jinja2 import TemplateNotFound
from sqlalchemy.exc import OperationalError
from werkzeug.exceptions import HTTPException
from .utils import safe_db_rollback
from .extensions import db
JSON_MIMETYPES = ["application/json", "text/html"]
def _safe_rollback() -> None:
try:
db.session.rollback()
except Exception:
pass
def _wants_json_response() -> bool:
if request.path.startswith("/api/"):
return True
if request.is_json:
return True
best = request.accept_mimetypes.best_match(JSON_MIMETYPES)
best = request.accept_mimetypes.best_match(["application/json", "text/html"])
if not best:
return False
@@ -30,43 +30,49 @@ def _wants_json_response() -> bool:
)
def _status_phrase(status_code: int) -> str:
def _get_status_phrase(status_code: int) -> str:
try:
return HTTPStatus(status_code).phrase
except ValueError:
return "Blad"
def _status_description(status_code: int) -> str:
def _get_status_description(status_code: int) -> str:
try:
return HTTPStatus(status_code).description
except ValueError:
return "Wystapil blad podczas przetwarzania zadania."
def _plain_fallback(status_code: int, phrase: str, description: str):
html = f"""<!doctype html>
<html lang=\"pl\">
<head>
<meta charset=\"utf-8\">
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">
<title>{status_code} {escape(phrase)}</title>
</head>
<body>
<h1>{status_code} - {escape(phrase)}</h1>
<p>{escape(description)}</p>
</body>
</html>"""
return html, status_code
def _error_headers(status_code: int) -> dict[str, str]:
headers = {}
if status_code >= 500:
headers.update(
{
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0, private",
"Pragma": "no-cache",
"Expires": "0",
"Surrogate-Control": "no-store",
}
)
return headers
def _render_error(status_code: int, message: str | None = None):
phrase = _status_phrase(status_code)
description = message or _status_description(status_code)
payload = {"status": status_code, "error": phrase, "message": description}
phrase = _get_status_phrase(status_code)
description = message or _get_status_description(status_code)
headers = _error_headers(status_code)
payload = {
"status": status_code,
"error": phrase,
"message": description,
}
if _wants_json_response():
return jsonify(payload), status_code
return jsonify(payload), status_code, headers
try:
return (
@@ -77,11 +83,14 @@ def _render_error(status_code: int, message: str | None = None):
error_message=description,
),
status_code,
headers,
)
except TemplateNotFound:
return _plain_fallback(status_code, phrase, description)
except Exception:
return _plain_fallback(status_code, phrase, description)
return (
f"{status_code} {phrase}: {description}",
status_code,
headers,
)
def register_error_handlers(app):
@@ -91,12 +100,15 @@ def register_error_handlers(app):
@app.errorhandler(OperationalError)
def handle_operational_error(exc):
safe_db_rollback()
_safe_rollback()
app.logger.exception("Blad polaczenia z baza danych: %s", exc)
return _render_error(503, "Baza danych jest chwilowo niedostepna. Sprobuj ponownie za chwile.")
return _render_error(
503,
"Baza danych jest chwilowo niedostepna. Sprobuj ponownie za chwile.",
)
@app.errorhandler(Exception)
def handle_unexpected_error(exc):
safe_db_rollback()
_safe_rollback()
app.logger.exception("Nieobsluzony wyjatek: %s", exc)
return _render_error(500, "Wystapil nieoczekiwany blad serwera.")
return _render_error(500, "Wystapil nieoczekiwany blad serwera.")

View File

@@ -1,21 +1,75 @@
{% extends "base.html" %}
<!doctype html>
<html lang="pl">
<head>
<meta charset="utf-8">
<title>{{ error_code }} {{ error_name }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
{% block title %}{{ error_code }} {{ error_name }}{% endblock %}
{% if asset_url is defined %}
<link rel="stylesheet" href="{{ asset_url('css/style.css') }}">
{% else %}
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
{% endif %}
{% block content %}
<div class="row justify-content-center py-5">
<div class="col-12 col-md-10 col-lg-8">
<div class="card border-warning shadow-sm">
<div class="card-body p-4 p-md-5 text-center">
<div class="display-4 fw-bold mb-3">{{ error_code }}</div>
<h1 class="h3 mb-3">{{ error_name }}</h1>
<p class="lead mb-4">{{ error_message }}</p>
<div class="d-flex gap-2 justify-content-center flex-wrap">
<a href="{{ url_for('index') }}" class="btn btn-primary">Wroc na strone glowna</a>
<a href="javascript:history.back()" class="btn btn-outline-light">Wroc</a>
</div>
<style>
body {
margin: 0;
padding: 0;
}
.error-page {
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
padding: 2rem 1rem;
}
.error-box {
width: 100%;
max-width: 720px;
text-align: center;
padding: 2rem;
border-radius: 12px;
background: #fff;
box-shadow: 0 8px 30px rgba(0,0,0,.08);
}
.error-code {
font-size: 3rem;
font-weight: 700;
margin-bottom: 1rem;
}
.error-name {
font-size: 1.5rem;
margin-bottom: 1rem;
}
.error-message {
margin-bottom: 1.5rem;
opacity: .9;
}
.error-actions a {
display: inline-block;
padding: .75rem 1.25rem;
text-decoration: none;
border-radius: 8px;
}
</style>
</head>
<body>
<main class="error-page">
<section class="error-box">
<div class="error-code">{{ error_code }}</div>
<h1 class="error-name">{{ error_name }}</h1>
<p class="error-message">{{ error_message }}</p>
<div class="error-actions">
<a href="/">Powrot na strone glowna</a>
</div>
</div>
</div>
</div>
{% endblock %}
</section>
</main>
</body>
</html>