Skip to content

Commit

Permalink
feat(server): handle snowflake ProgrammingError
Browse files Browse the repository at this point in the history
  • Loading branch information
tekumara committed Aug 25, 2024
1 parent e98d227 commit 9455a43
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
19 changes: 17 additions & 2 deletions fakesnow/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from base64 import b64encode
from dataclasses import dataclass

import snowflake.connector.errors
from starlette.applications import Starlette
from starlette.concurrency import run_in_threadpool
from starlette.requests import Request
Expand Down Expand Up @@ -45,8 +46,22 @@ async def query_request(request: Request) -> JSONResponse:

sql_text = body_json["sqlText"]

# only a single sql statement is sent at a time by the python snowflake connector
cur = await run_in_threadpool(conn.cursor().execute, sql_text)
try:
# only a single sql statement is sent at a time by the python snowflake connector
cur = await run_in_threadpool(conn.cursor().execute, sql_text)
except snowflake.connector.errors.ProgrammingError as e:
code = f"{e.errno:06d}"
return JSONResponse(
{
"data": {
"errorCode": code,
"sqlState": e.sqlstate,
},
"code": code,
"message": e.msg,
"success": False,
}
)

rowtype = describe_as_rowtype(cur._describe_last_sql()) # noqa: SLF001

Expand Down
11 changes: 11 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,14 @@ def test_server_abort_request(server: dict) -> None:
conn1.cursor() as cur,
):
cur.execute("select 'will abort'")


def test_server_errors(scur: snowflake.connector.cursor.SnowflakeCursor) -> None:
cur = scur
with pytest.raises(snowflake.connector.errors.ProgrammingError) as excinfo:
cur.execute("select * from this_table_does_not_exist")

assert excinfo.value.errno == 2003
assert excinfo.value.sqlstate == "42S02"
assert excinfo.value.msg
assert "THIS_TABLE_DOES_NOT_EXIST" in excinfo.value.msg

0 comments on commit 9455a43

Please sign in to comment.