diff --git a/src/databricks/sql/exc.py b/src/databricks/sql/exc.py index 9e918a936..f043253c5 100644 --- a/src/databricks/sql/exc.py +++ b/src/databricks/sql/exc.py @@ -37,10 +37,10 @@ def __init__( ) def __str__(self): - return self.message + return self.message or "" def message_with_context(self): - return self.message + ": " + json.dumps(self.context, default=str) + return (self.message or "") + ": " + json.dumps(self.context, default=str) class Warning(Exception): diff --git a/tests/unit/test_exc.py b/tests/unit/test_exc.py new file mode 100644 index 000000000..7efe1a081 --- /dev/null +++ b/tests/unit/test_exc.py @@ -0,0 +1,19 @@ +import databricks.sql.exc as exc + + +class TestError: + def test_str_returns_string_when_message_is_none(self): + """object.__str__ must return a str; a None message must not raise + TypeError: __str__ returned non-string (type NoneType).""" + e = exc.Error() + result = str(e) + assert isinstance(result, str) + + def test_message_with_context_when_message_is_none(self): + """message_with_context must not raise on `None + str` when message + is None; it should still serialize the context.""" + e = exc.Error(context={"foo": "bar"}) + result = e.message_with_context() + assert isinstance(result, str) + assert "foo" in result + assert "bar" in result