feat: error boundary
This commit is contained in:
parent
db0cd90237
commit
d1db42fd58
38
backend.py
38
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():
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user