error handler
This commit is contained in:
23
app.py
23
app.py
@@ -43,6 +43,7 @@ from flask_socketio import SocketIO, emit, join_room
|
||||
from config import Config
|
||||
from PIL import Image, ExifTags, ImageFilter, ImageOps
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
from werkzeug.exceptions import InternalServerError
|
||||
from sqlalchemy import func, extract, inspect, or_, case, text
|
||||
from sqlalchemy.orm import joinedload
|
||||
from collections import defaultdict, deque
|
||||
@@ -260,10 +261,8 @@ class Receipt(db.Model):
|
||||
shopping_list = db.relationship("ShoppingList", back_populates="receipts")
|
||||
|
||||
|
||||
@app.errorhandler(OperationalError)
|
||||
def handle_db_error(e):
|
||||
app.logger.error(f"[Błąd DB] {e}")
|
||||
|
||||
def render_db_unavailable():
|
||||
app.logger.error("[Błąd DB] Baza danych niedostępna")
|
||||
if request.accept_mimetypes.best == "application/json":
|
||||
return (
|
||||
jsonify(
|
||||
@@ -285,6 +284,22 @@ def handle_db_error(e):
|
||||
)
|
||||
|
||||
|
||||
@app.errorhandler(OperationalError)
|
||||
def handle_db_error(e):
|
||||
app.logger.error(f"[Błąd DB] {e}")
|
||||
return render_db_unavailable()
|
||||
|
||||
|
||||
@app.errorhandler(Exception)
|
||||
def handle_any_exception(e):
|
||||
orig_exc = getattr(e, "original_exception", None)
|
||||
if isinstance(e, OperationalError) or isinstance(orig_exc, OperationalError):
|
||||
app.logger.error(f"[Błąd DB] {orig_exc or e}")
|
||||
return render_db_unavailable()
|
||||
|
||||
raise e
|
||||
|
||||
|
||||
def hash_password(password):
|
||||
pepper = app.config["BCRYPT_PEPPER"]
|
||||
peppered = (password + pepper).encode("utf-8")
|
||||
|
Reference in New Issue
Block a user