Spaces:
Sleeping
Sleeping
500 error
Browse files
app.py
CHANGED
|
@@ -5,6 +5,8 @@ from flask import Flask, request, jsonify, Response, stream_with_context
|
|
| 5 |
from flask_cors import CORS
|
| 6 |
import requests
|
| 7 |
from datetime import timedelta
|
|
|
|
|
|
|
| 8 |
|
| 9 |
from flask_jwt_extended import (
|
| 10 |
JWTManager,
|
|
@@ -16,6 +18,30 @@ from flask_jwt_extended import (
|
|
| 16 |
app = Flask(__name__)
|
| 17 |
CORS(app)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
# Strong secret key via HF Space secret
|
| 20 |
app.config["JWT_SECRET_KEY"] = os.environ.get("JWT_SECRET_KEY")
|
| 21 |
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(hours=24)
|
|
|
|
| 5 |
from flask_cors import CORS
|
| 6 |
import requests
|
| 7 |
from datetime import timedelta
|
| 8 |
+
from werkzeug.exceptions import HTTPException
|
| 9 |
+
import uuid
|
| 10 |
|
| 11 |
from flask_jwt_extended import (
|
| 12 |
JWTManager,
|
|
|
|
| 18 |
app = Flask(__name__)
|
| 19 |
CORS(app)
|
| 20 |
|
| 21 |
+
|
| 22 |
+
@app.errorhandler(Exception)
|
| 23 |
+
def _handle_unexpected_exception(e: Exception):
|
| 24 |
+
"""Return JSON for uncaught exceptions (avoids HTML 500 responses)."""
|
| 25 |
+
request_id = uuid.uuid4().hex[:12]
|
| 26 |
+
try:
|
| 27 |
+
app.logger.exception("Unhandled exception request_id=%s", request_id)
|
| 28 |
+
except Exception:
|
| 29 |
+
pass
|
| 30 |
+
|
| 31 |
+
status_code = 500
|
| 32 |
+
public_message = "Internal Server Error"
|
| 33 |
+
if isinstance(e, HTTPException):
|
| 34 |
+
status_code = int(getattr(e, "code", 500) or 500)
|
| 35 |
+
public_message = getattr(e, "description", public_message) or public_message
|
| 36 |
+
|
| 37 |
+
payload: dict = {"ok": False, "error": public_message, "request_id": request_id}
|
| 38 |
+
|
| 39 |
+
if (os.environ.get("EXPOSE_ERRORS") or "").strip() == "1":
|
| 40 |
+
payload["type"] = type(e).__name__
|
| 41 |
+
payload["details"] = str(e)
|
| 42 |
+
|
| 43 |
+
return jsonify(payload), status_code
|
| 44 |
+
|
| 45 |
# Strong secret key via HF Space secret
|
| 46 |
app.config["JWT_SECRET_KEY"] = os.environ.get("JWT_SECRET_KEY")
|
| 47 |
app.config["JWT_ACCESS_TOKEN_EXPIRES"] = timedelta(hours=24)
|