From d1db42fd58e0c15c1e2b179c9d223185e6505793 Mon Sep 17 00:00:00 2001 From: Dustella Date: Sat, 28 Jun 2025 12:10:21 +0800 Subject: [PATCH] feat: error boundary --- backend.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/backend.py b/backend.py index aff8663..2fb3c4b 100644 --- a/backend.py +++ b/backend.py @@ -1,11 +1,12 @@ import os +import traceback from matplotlib import pyplot as plt from modules import cosmic from modules import tidi from modules import saber from modules import radar from modules import balloon -from quart import Quart, request +from quart import Quart, jsonify, request from quart_cors import cors from typing import get_args import sys @@ -42,6 +43,41 @@ def auth(): if _code != code: return "Unauthorized", 401 +# Global error handler for all exceptions + + +@app.errorhandler(Exception) +async def handle_exception(e): + """Global error handler that catches all exceptions""" + # Get the error details + error_class = e.__class__.__name__ + error_message = str(e) + + # Get stack trace + stack_trace = traceback.format_exc() + + # Log the error (you might want to use a proper logger) + print(f"Exception occurred: {error_class} - {error_message}") + print(f"Stack Trace: {stack_trace}") + + # Determine appropriate status code + status_code = 500 # Default to internal server error + + # Return a consistent error response + response = { + "success": False, + "error": { + "type": 500, + "message": error_message + } + } + + # Only include stack trace in debug mode (not in production) + if app.debug: + response["error"]["stack_trace"] = stack_trace.split('\n') + + return jsonify(response), status_code + @app.route('/') async def return_index_html():