From ad184b8d2b3b791840ee8365de47e5694b71d048 Mon Sep 17 00:00:00 2001 From: Daniel Kerbel Date: Tue, 30 Jul 2024 15:56:53 +0300 Subject: [PATCH] Make error handling more robust to python configuration --- jupyter_server/base/handlers.py | 9 +++------ jupyter_server/utils.py | 9 +++++++++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/jupyter_server/base/handlers.py b/jupyter_server/base/handlers.py index 6f2bbda040..6ceae2780b 100644 --- a/jupyter_server/base/handlers.py +++ b/jupyter_server/base/handlers.py @@ -11,7 +11,6 @@ import mimetypes import os import re -import sqlite3 import types import warnings from http.client import responses @@ -37,6 +36,7 @@ from jupyter_server.utils import ( ensure_async, filefind, + is_sqlite_disk_full_error, url_escape, url_is_absolute, url_path_join, @@ -766,12 +766,9 @@ def write_error(self, status_code: int, **kwargs: Any) -> None: if isinstance(e, HTTPError): reply["message"] = e.log_message or message reply["reason"] = e.reason - elif ( - isinstance(e, sqlite3.OperationalError) - and e.sqlite_errorcode == sqlite3.SQLITE_FULL - ): + elif is_sqlite_disk_full_error(e): reply["message"] = "Disk is full" - reply["reason"] = e.sqlite_errorname + reply["reason"] = str(e) else: reply["message"] = "Unhandled error" reply["reason"] = None diff --git a/jupyter_server/utils.py b/jupyter_server/utils.py index 0c987bff25..ff34e55faa 100644 --- a/jupyter_server/utils.py +++ b/jupyter_server/utils.py @@ -433,3 +433,12 @@ class JupyterServerAuthWarning(RuntimeWarning): Intended for filtering out expected warnings in tests, including downstream tests, rather than for users to silence this warning. """ + + +def is_sqlite_disk_full_error(e: Exception) -> bool: + try: + import sqlite3 + + return isinstance(e, sqlite3.OperationalError) and e.sqlite_errorcode == sqlite3.SQLITE_FULL # type: ignore[attr-defined] + except (AttributeError, ImportError) as e: + return "database or disk is full" in str(e)