Skip to content

Commit b7e57ff

Browse files
author
peco-engineer-bot[bot]
committed
Bug in databricks.sql.exc.Error base class (#437)
Signed-off-by: peco-engineer-bot[bot] <3815206+peco-engineer-bot[bot]@users.noreply.github.com>
1 parent 9775996 commit b7e57ff

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

src/databricks/sql/exc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ def __init__(
3737
)
3838

3939
def __str__(self):
40-
return self.message
40+
return self.message or ""
4141

4242
def message_with_context(self):
43-
return self.message + ": " + json.dumps(self.context, default=str)
43+
return (self.message or "") + ": " + json.dumps(self.context, default=str)
4444

4545

4646
class Warning(Exception):

tests/unit/test_exc.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import databricks.sql.exc as exc
2+
3+
4+
class TestError:
5+
def test_str_returns_string_when_message_is_none(self):
6+
"""object.__str__ must return a str; a None message must not raise
7+
TypeError: __str__ returned non-string (type NoneType)."""
8+
e = exc.Error()
9+
result = str(e)
10+
assert isinstance(result, str)
11+
12+
def test_message_with_context_when_message_is_none(self):
13+
"""message_with_context must not raise on `None + str` when message
14+
is None; it should still serialize the context."""
15+
e = exc.Error(context={"foo": "bar"})
16+
result = e.message_with_context()
17+
assert isinstance(result, str)
18+
assert "foo" in result
19+
assert "bar" in result

0 commit comments

Comments
 (0)