now errors are sent with json form instead of html

This commit is contained in:
Karma Riuk
2025-05-14 13:18:36 +02:00
parent 3a4bfd611b
commit 741c2a95e6

View File

@ -5,7 +5,7 @@ from flask_socketio import SocketIO
from routes.index import router as index_router
from routes.answers import router as answers_router
from routes.datasets import router as datasets_router
from werkzeug.exceptions import HTTPException
import os
app = Flask(__name__, static_folder='../public', static_url_path='/')
@ -17,6 +17,22 @@ app.register_blueprint(answers_router) # mounts at '/answers'
app.register_blueprint(datasets_router) # mounts at '/datasets'
@app.errorhandler(Exception)
def handle_exception(e):
if isinstance(e, HTTPException):
response = {
"error": e.name.lower().replace(" ", "_"), # e.g. "not_found"
"message": e.description,
}
return app.json.response(response), e.code or 500
app.logger.exception(e)
return (
app.json.response({"error": "internal_server_error", "message": str(e)}),
500,
)
def init_socketio(app):
socketio = SocketIO(app, cors_allowed_origins='*')