From ee067c0c6f9a9655d429e87b8e33d03a6ec3e5b6 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 | 13 +++++++++++++ 2 files changed, 16 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..6520995f07 100644 --- a/jupyter_server/utils.py +++ b/jupyter_server/utils.py @@ -433,3 +433,16 @@ 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: + try: + import sqlite3 + except ImportError: + # fallback on pysqlite2 if Python was build without sqlite + from pysqlite2 import dbapi2 as sqlite3 # type:ignore[no-redef] + + return isinstance(e, sqlite3.OperationalError) and e.sqlite_errorcode == sqlite3.SQLITE_FULL + except AttributeError: + return "database or disk is full" in str(e)