Skip to content

Commit b7d4af2

Browse files
committed
adjusting error handling for fastapi
1 parent 8b58cf4 commit b7d4af2

2 files changed

Lines changed: 22 additions & 15 deletions

File tree

dash/backends/_fastapi.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,29 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: #
7070
CONFIG_PATH = "dash_config.json"
7171

7272

73-
def save_config(config):
74-
with open(CONFIG_PATH, "w") as f:
75-
json.dump(config, f)
73+
# Internal config helpers (local to this file)
74+
_CONFIG_PATH = "dash_config.json"
7675

76+
def _save_config(config):
77+
with open(_CONFIG_PATH, "w") as f:
78+
json.dump(config, f)
7779

78-
def load_config():
79-
if os.path.exists(CONFIG_PATH):
80-
with open(CONFIG_PATH, "r") as f:
81-
return json.load(f)
80+
def _load_config():
81+
try:
82+
if os.path.exists(_CONFIG_PATH):
83+
with open(_CONFIG_PATH, "r") as f:
84+
return json.load(f)
85+
except Exception:
86+
pass # ignore errors
8287
return {}
8388

8489

8590
class FastAPIDashServer(BaseDashServer):
8691
def __init__(self, server: FastAPI):
87-
self.config = {}
92+
_save_config({"debug": False}) # ensure config file exists
8893
self.server_type = "fastapi"
8994
self.server: FastAPI = server
90-
self.error_handling_mode = "prune"
95+
self.error_handling_mode = "ignore"
9196
self.request_adapter = FastAPIRequestAdapter
9297
super().__init__()
9398

@@ -120,7 +125,7 @@ def register_assets_blueprint(
120125
pass
121126

122127
def register_error_handlers(self):
123-
self.error_handling_mode = "prune"
128+
self.error_handling_mode = "ignore"
124129

125130
def _get_traceback(self, _secret, error: Exception):
126131
tb = error.__traceback__
@@ -256,7 +261,7 @@ def setup_catchall(self, dash_app: Dash):
256261
@self.server.on_event("startup")
257262
def _setup_catchall():
258263
dash_app.enable_dev_tools(
259-
**load_config(), first_run=False
264+
**_load_config(), first_run=False
260265
) # do this to make sure dev tools are enabled
261266

262267
async def catchall(request: Request):
@@ -298,12 +303,12 @@ def after_request(self, func: Callable[[], Any] | None):
298303
def run(self, dash_app: Dash, host, port, debug, **kwargs):
299304
frame = inspect.stack()[2]
300305
config = dict(
301-
{"debug": debug} if debug else {},
306+
{"debug": debug} if debug else {"debug": False},
302307
**{
303308
f"dev_tools_{k}": v for k, v in dash_app._dev_tools.items()
304309
}, # pylint: disable=protected-access
305310
)
306-
save_config(config)
311+
_save_config(config)
307312
if debug:
308313
if kwargs.get("reload") is None:
309314
kwargs["reload"] = True
@@ -352,7 +357,7 @@ async def middleware(request, call_next):
352357
return Response(content=tb, media_type="text/html", status_code=500)
353358
return JSONResponse(
354359
status_code=500,
355-
content={"error": "InternalServerError", "message": str(e.args[0])},
360+
content={"error": "InternalServerError", "message": "An internal server error occurred."},
356361
)
357362

358363
return middleware

dash/backends/_quart.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, server: Quart) -> None:
3434
self.server_type = "quart"
3535
self.server: Quart = server
3636
self.config = {}
37-
self.error_handling_mode = "prune"
37+
self.error_handling_mode = "ignore"
3838
self.request_adapter = QuartRequestAdapter
3939
super().__init__()
4040

@@ -184,6 +184,8 @@ def register_prune_error_handler(self, secret, prune_errors):
184184

185185
@self.server.errorhandler(Exception)
186186
async def _wrap_errors(error):
187+
if self.error_handling_mode == "ignore":
188+
return Response("Internal server error.", status=500, content_type="text/plain")
187189
tb = self._get_traceback(secret, error)
188190
return Response(tb, status=500, content_type="text/html")
189191

0 commit comments

Comments
 (0)