Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: dont show python traceback for sql errors (#6573)

Myles Scolnickd368d41224bf
diff --git a/marimo/_runtime/runner/cell_runner.py b/marimo/_runtime/runner/cell_runner.py
index 78e2913c8d..34eff08b7d 100644
--- a/marimo/_runtime/runner/cell_runner.py
+++ b/marimo/_runtime/runner/cell_runner.py
@@ -22,6 +22,7 @@ from marimo._loggers import marimo_logger
from marimo._messaging.errors import (
Error,
MarimoExceptionRaisedError,
+ MarimoSQLError,
MarimoStrictExecutionError,
UnknownError,
)
@@ -513,10 +514,26 @@ class Runner:
# this call as well, so this should be lifted out of `run`.
self.cancel(cell_id)
- # Stop "errors" aren't actually errors but rather a control
- # flow mechanism used by mo.stop() to stop execution; as such
- # a traceback should not be shown for them.
- if not isinstance(run_result.exception, MarimoStopError):
+ def should_show_traceback(
+ exception: Optional[ErrorObjects],
+ ) -> bool:
+ if exception is None:
+ return True
+
+ # Stop "errors" aren't actually errors but rather a control
+ # flow mechanism used by mo.stop() to stop execution; as such
+ # a traceback should not be shown for them.
+ if isinstance(exception, MarimoStopError):
+ return False
+
+ # SQL parsing errors happen in SQL cells so showing a
+ # python traceback is not useful.
+ if isinstance(exception, MarimoSQLError):
+ return False
+
+ return True
+
+ if should_show_traceback(run_result.exception):
tmpio = io.StringIO()
# The executors explicitly raise cell exceptions from base
# exceptions such that the stack trace is cleaner.
diff --git a/tests/_runtime/runner/test_cell_runner.py b/tests/_runtime/runner/test_cell_runner.py
index 3151394135..b7e67bdf9b 100644
--- a/tests/_runtime/runner/test_cell_runner.py
+++ b/tests/_runtime/runner/test_cell_runner.py
@@ -1,6 +1,10 @@
# Copyright 2024 Marimo. All rights reserved.
import traceback
+import pytest
+
+from marimo._dependencies.dependencies import DependencyManager
+from marimo._messaging.errors import MarimoSQLError
from marimo._runtime.capture import capture_stderr
from marimo._runtime.runner.cell_runner import Runner
from marimo._runtime.runtime import Kernel
@@ -98,3 +102,38 @@ async def test_base_exception_caught(
assert "line 3" in "\n".join(
traceback.format_tb(k.debugger._last_traceback)
)
+
+
+ not DependencyManager.sqlglot.has(), reason="SQLGlot not installed"
+)
+async def test_sql_parse_error_suppresses_python_traceback(
+ execution_kernel: Kernel, exec_req: ExecReqProvider
+) -> None:
+ k = execution_kernel
+ # first run the cell to populate the graph
+ await k.run(
+ [
+ er := exec_req.get_with_id(
+ "sql-1",
+ """
+from sqlglot.errors import ParseError
+raise ParseError("malformed SQL")
+""",
+ )
+ ]
+ )
+
+ runner = Runner(
+ roots=set(k.graph.cells.keys()),
+ graph=k.graph,
+ glbls=k.globals,
+ debugger=k.debugger,
+ )
+
+ with capture_stderr() as buffer:
+ await runner.run(er.cell_id)
+
+ stderr = buffer.getvalue()
+ assert "Traceback (most recent call last):" not in stderr
+ assert isinstance(runner.exceptions[er.cell_id], MarimoSQLError)