Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions src/firebolt/utils/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from firebolt.utils.exception import (
ConfigurationError,
FireboltError,
FireboltStructuredError,
)

Expand Down Expand Up @@ -171,6 +172,10 @@ def raise_error_from_response(resp: Response) -> None:
resp (Response): HTTP response
"""
to_raise = None
# If error is Text - raise as is
if "text/plain" in resp.headers.get("Content-Type", ""):
raise FireboltError(resp.text)
# If error is Json - parse it and raise
try:
decoded = resp.json()
if "errors" in decoded and len(decoded["errors"]) > 0:
Expand All @@ -186,6 +191,7 @@ def raise_error_from_response(resp: Response) -> None:
raise to_raise

# Raise status error if no error info was found in the body
# This error does not contain the response body
resp.raise_for_status()


Expand Down
4 changes: 2 additions & 2 deletions tests/integration/dbapi/async/V2/test_queries_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from firebolt.client.auth.base import Auth
from firebolt.common._types import ColType
from firebolt.common.row_set.types import Column
from firebolt.utils.exception import FireboltStructuredError
from firebolt.utils.exception import FireboltError, FireboltStructuredError
from tests.integration.dbapi.conftest import LONG_SELECT_DEFAULT_V2
from tests.integration.dbapi.utils import assert_deep_eq

Expand Down Expand Up @@ -341,7 +341,7 @@ async def test_multi_statement_query(connection: Connection) -> None:
async def test_set_invalid_parameter(connection: Connection):
async with connection.cursor() as c:
assert len(c._set_parameters) == 0
with raises((OperationalError, FireboltStructuredError)) as e:
with raises((OperationalError, FireboltError)) as e:
await c.execute("SET some_invalid_parameter = 1")

assert "Unknown setting" in str(e.value) or "query param not allowed" in str(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/dbapi/sync/V2/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from firebolt.common._types import ColType
from firebolt.common.row_set.types import Column
from firebolt.db import Binary, Connection, Cursor, OperationalError, connect
from firebolt.utils.exception import FireboltStructuredError
from firebolt.utils.exception import FireboltError, FireboltStructuredError
from tests.integration.dbapi.conftest import LONG_SELECT_DEFAULT_V2
from tests.integration.dbapi.utils import assert_deep_eq

Expand Down Expand Up @@ -341,7 +341,7 @@ def test_multi_statement_query(connection: Connection) -> None:
def test_set_invalid_parameter(connection: Connection):
with connection.cursor() as c:
assert len(c._set_parameters) == 0
with raises((OperationalError, FireboltStructuredError)) as e:
with raises((OperationalError, FireboltError)) as e:
c.execute("set some_invalid_parameter = 1")
assert "Unknown setting" in str(e.value) or "query param not allowed" in str(
e.value
Expand Down
23 changes: 23 additions & 0 deletions tests/unit/async_db/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1505,3 +1505,26 @@ async def test_unsupported_paramstyle_raises(cursor: Cursor) -> None:
await cursor.execute("SELECT 1")
finally:
db.paramstyle = original_paramstyle


async def test_cursor_plaintext_error(
httpx_mock: HTTPXMock,
cursor: Cursor,
query_url: str,
):
"""Test handling of plaintext error responses from the server."""
httpx_mock.add_callback(
lambda *args, **kwargs: Response(
status_code=codes.NOT_FOUND,
text="Plaintext error message",
headers={"Content-Type": "text/plain"},
),
url=query_url,
)
with raises(FireboltError) as excinfo:
await cursor.execute("select * from t")

assert cursor._state == CursorState.ERROR
assert "Plaintext error message" in str(
excinfo.value
), "Invalid error message for plaintext error response"
23 changes: 23 additions & 0 deletions tests/unit/db/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1391,3 +1391,26 @@ def test_unsupported_paramstyle_raises(cursor):
cursor.execute("SELECT 1")
finally:
db.paramstyle = original_paramstyle


def test_cursor_plaintext_error(
httpx_mock: HTTPXMock,
cursor: Cursor,
query_url: str,
):
"""Test handling of plaintext error responses from the server."""
httpx_mock.add_callback(
lambda *args, **kwargs: Response(
status_code=codes.NOT_FOUND,
text="Plaintext error message",
headers={"Content-Type": "text/plain"},
),
url=query_url,
)
with raises(FireboltError) as excinfo:
cursor.execute("select * from t")

assert cursor._state == CursorState.ERROR
assert "Plaintext error message" in str(
excinfo.value
), "Invalid error message for plaintext error response"
Loading