From 02649e947f51a807a5c97be4347f43a252838a5f Mon Sep 17 00:00:00 2001 From: Alex Carney Date: Tue, 16 Jul 2024 20:05:51 +0000 Subject: [PATCH] sphinx-agent: Handle unexpected config values --- lib/esbonio/changes/843.fix.md | 1 + lib/esbonio/esbonio/sphinx_agent/app.py | 1 + .../esbonio/sphinx_agent/handlers/files.py | 26 +++++++++++++------ lib/esbonio/tests/workspaces/demo/conf.py | 4 +++ 4 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 lib/esbonio/changes/843.fix.md diff --git a/lib/esbonio/changes/843.fix.md b/lib/esbonio/changes/843.fix.md new file mode 100644 index 00000000..ca2e6f32 --- /dev/null +++ b/lib/esbonio/changes/843.fix.md @@ -0,0 +1 @@ +The sphinx agent should no longer crash when encountering unexpected config values diff --git a/lib/esbonio/esbonio/sphinx_agent/app.py b/lib/esbonio/esbonio/sphinx_agent/app.py index 2b3e65bb..644baf58 100644 --- a/lib/esbonio/esbonio/sphinx_agent/app.py +++ b/lib/esbonio/esbonio/sphinx_agent/app.py @@ -28,6 +28,7 @@ RoleDefinition = Tuple[str, Any, List[types.Role.TargetProvider]] sphinx_logger = logging.getLogger(SPHINX_LOG_NAMESPACE) +logger = sphinx_logger.getChild("esbonio") sphinx_log_setup = sphinx_logging_module.setup diff --git a/lib/esbonio/esbonio/sphinx_agent/handlers/files.py b/lib/esbonio/esbonio/sphinx_agent/handlers/files.py index cccd470a..eeab0909 100644 --- a/lib/esbonio/esbonio/sphinx_agent/handlers/files.py +++ b/lib/esbonio/esbonio/sphinx_agent/handlers/files.py @@ -4,10 +4,12 @@ from ..app import Database from ..app import Sphinx +from ..app import logger from ..types import Uri from ..util import as_json if typing.TYPE_CHECKING: + from typing import Any from typing import List from typing import Optional from typing import Tuple @@ -45,6 +47,16 @@ def init_db(app: Sphinx, config: Config): app.esbonio.db.ensure_table(CONFIG_TABLE) +def value_to_db(name: str, item: Any) -> Tuple[str, str, Any]: + """Convert a single value to its DB representation""" + + try: + (value, scope, _) = item + return (name, scope, as_json(value)) + except Exception: + return (name, "", as_json(item)) + + def dump_config(app: Sphinx, *args): """Dump the user's config into the db so that the parent language server can inspect it.""" @@ -61,20 +73,18 @@ def dump_config(app: Sphinx, *args): continue try: - (value, scope, _) = item - values.append((name, scope, as_json(value))) - except Exception: - values.append((name, "", as_json(item))) + values.append(value_to_db(name, item)) + except Exception as exc: + logger.debug(f"Unable to dump config value: {name!r}: {exc}") for name, item in config.items(): if name in IGNORED_CONFIG_NAMES: continue try: - (value, scope, _) = item - values.append((name, scope, as_json(value))) - except Exception: - values.append((name, "", as_json(item))) + values.append(value_to_db(name, item)) + except Exception as exc: + logger.debug(f"Unable to dump config value: {name!r}: {exc}") app.esbonio.db.insert_values(CONFIG_TABLE, values) diff --git a/lib/esbonio/tests/workspaces/demo/conf.py b/lib/esbonio/tests/workspaces/demo/conf.py index bbd74a0e..430f82ba 100644 --- a/lib/esbonio/tests/workspaces/demo/conf.py +++ b/lib/esbonio/tests/workspaces/demo/conf.py @@ -24,6 +24,10 @@ templates_path = ["_templates"] exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", "env", ".tox", "README.md"] +linkcheck_allowed_redirects = { + # All HTTP redirections from the source URI to the canonical URI will be treated as "working". + r"https://sphinx-doc\.org/.*": r"https://sphinx-doc\.org/en/master/.*" +} # -- Options for HTML output ------------------------------------------------- # https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output