Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNOW-701257: add query attribute to Error class #1437

Merged
merged 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/snowflake/connector/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ def execute(
"errno": int(code),
"sqlstate": self._sqlstate,
"sfqid": self._sfqid,
"query": query,
}
is_integrity_error = (
code == "100072"
Expand Down
7 changes: 5 additions & 2 deletions src/snowflake/connector/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __init__(
errno: int | None = None,
sqlstate: str | None = None,
sfqid: str | None = None,
query: str | None = None,
done_format_msg: bool | None = None,
connection: SnowflakeConnection | None = None,
cursor: SnowflakeCursor | None = None,
Expand All @@ -48,6 +49,7 @@ def __init__(
self.errno = errno or -1
self.sqlstate = sqlstate or "n/a"
self.sfqid = sfqid
self.query = query

if self.msg:
# TODO: If there's a message then check to see if errno (and maybe sqlstate)
Expand All @@ -66,9 +68,9 @@ def __init__(
if self.sqlstate != "n/a":
if not already_formatted_msg:
if logger.getEffectiveLevel() in (logging.INFO, logging.DEBUG):
self.msg = f"{self.errno:06d} ({self.sqlstate}): {self.sfqid}: {self.msg}"
self.msg = f"{self.errno:06d} ({self.sqlstate}): {self.sfqid}: {self.msg}: {self.query}"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sfc-gh-mkeller could I get your input on whether this could be considered behavior change? We want to add query into Errors generated by python connector (more context here) so that query is easily accessible when debugging issues and does not get lost.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I don't think that this would be a behavior change, but I think it should be.
If you look at line 46 super().__init__(msg) you'll see that we call Exceptions's initalizer with the initially given msg, so this later isn't a behavior change. But I'm not sure that right now it works as it should. I verified this with:

❯ python
Python 3.9.16 (main, Dec  7 2022, 10:06:04)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from snowflake.connector.errors import Error
/Users/mkeller/snowflakedb/snowflake-connector-python/src/snowflake/connector/options.py:13: UserWarning: Module snowflake was already imported from None, but /Users/mkeller/snowflakedb/snowflake-connector-python/src is being added to sys.path
  import pkg_resources
>>> e = Error("this is a test error", errno=111)
>>> e.args
('this is a test error',)
>>> e.msg
'000111: this is a test error'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However; it does affect __str__

>>> str(e)
'000111: this is a test error'

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a middle ground could we not add the query to msg but have it as a field of Error? For the short-term at least

else:
self.msg = f"{self.errno:06d} ({self.sqlstate}): {self.msg}"
self.msg = f"{self.errno:06d} ({self.sqlstate}): {self.msg}: {self.query}"
else:
if not already_formatted_msg:
if logger.getEffectiveLevel() in (logging.INFO, logging.DEBUG):
Expand Down Expand Up @@ -221,6 +223,7 @@ def default_errorhandler(
errno=None if errno is None else int(errno),
sqlstate=error_value.get("sqlstate"),
sfqid=error_value.get("sfqid"),
query=error_value.get("query"),
done_format_msg=(
None if done_format_msg is None else bool(done_format_msg)
),
Expand Down
5 changes: 4 additions & 1 deletion test/integ/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,25 @@ def test_error_classes(conn_cnx):
assert ctx.ProgrammingError == errors.ProgrammingError


@pytest.mark.skipolddriver
def test_error_code(conn_cnx):
"""Error code is included in the exception."""
syntax_errno = 1494
syntax_errno_old = 1003
syntax_sqlstate = "42601"
syntax_sqlstate_old = "42000"
query = "SELECT * FROOOM TEST"
with conn_cnx() as ctx:
with pytest.raises(errors.ProgrammingError) as e:
ctx.cursor().execute("SELECT * FROOOM TEST")
ctx.cursor().execute(query)
assert (
e.value.errno == syntax_errno or e.value.errno == syntax_errno_old
), "Syntax error code"
assert (
e.value.sqlstate == syntax_sqlstate
or e.value.sqlstate == syntax_sqlstate_old
), "Syntax SQL state"
assert e.value.query == query, "Query mismatch"
e.match(
rf"^({syntax_errno:06d} \({syntax_sqlstate}\)|{syntax_errno_old:06d} \({syntax_sqlstate_old}\)): "
)
Expand Down
4 changes: 3 additions & 1 deletion test/unit/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@

def test_detecting_duplicate_detail_insertion():
sfqid = str(uuid.uuid4())
query = "select something_really_buggy from buggy_table"
sqlstate = "24000"
errno = 123456
msg = "Some error happened"
expected_msg = re.compile(rf"{errno} \({sqlstate}\): {sfqid}: {msg}")
expected_msg = re.compile(rf"{errno} \({sqlstate}\): {sfqid}: {msg}: {query}")
original_ex = errors.ProgrammingError(
sqlstate=sqlstate,
sfqid=sfqid,
query=query,
errno=errno,
msg=msg,
)
Expand Down