Skip to content

Commit 5db6d01

Browse files
make error more descriptive on command failure
Signed-off-by: varun-edachali-dbx <[email protected]>
1 parent 2485a73 commit 5db6d01

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/databricks/sql/backend/sea/backend.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import re
66
from typing import Any, Dict, Tuple, List, Optional, Union, TYPE_CHECKING, Set
77

8-
from databricks.sql.backend.sea.models.base import ResultManifest
8+
from databricks.sql.backend.sea.models.base import ResultManifest, StatementStatus
99
from databricks.sql.backend.sea.utils.constants import (
1010
ALLOWED_SESSION_CONF_TO_DEFAULT_VALUES_MAP,
1111
ResultFormat,
@@ -398,8 +398,9 @@ def _response_to_result_set(
398398
)
399399

400400
def _check_command_not_in_failed_or_closed_state(
401-
self, state: CommandState, command_id: CommandId
401+
self, status: StatementStatus, command_id: CommandId
402402
) -> None:
403+
state = status.state
403404
if state == CommandState.CLOSED:
404405
raise DatabaseError(
405406
"Command {} unexpectedly closed server side".format(command_id),
@@ -408,8 +409,9 @@ def _check_command_not_in_failed_or_closed_state(
408409
},
409410
)
410411
if state == CommandState.FAILED:
412+
error = status.error
411413
raise ServerOperationError(
412-
"Command {} failed".format(command_id),
414+
"Command failed: {} {}".format(error.error_code, error.message),
413415
{
414416
"operation-id": command_id,
415417
},
@@ -423,16 +425,18 @@ def _wait_until_command_done(
423425
"""
424426

425427
final_response: Union[ExecuteStatementResponse, GetStatementResponse] = response
426-
427-
state = final_response.status.state
428428
command_id = CommandId.from_sea_statement_id(final_response.statement_id)
429429

430-
while state in [CommandState.PENDING, CommandState.RUNNING]:
430+
while final_response.status.state in [
431+
CommandState.PENDING,
432+
CommandState.RUNNING,
433+
]:
431434
time.sleep(self.POLL_INTERVAL_SECONDS)
432435
final_response = self._poll_query(command_id)
433-
state = final_response.status.state
434436

435-
self._check_command_not_in_failed_or_closed_state(state, command_id)
437+
self._check_command_not_in_failed_or_closed_state(
438+
final_response.status, command_id
439+
)
436440

437441
return final_response
438442

tests/unit/test_sea_backend.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SeaDatabricksClient,
1313
_filter_session_configuration,
1414
)
15+
from databricks.sql.backend.sea.models.base import ServiceError, StatementStatus
1516
from databricks.sql.backend.types import SessionId, CommandId, CommandState, BackendType
1617
from databricks.sql.parameters.native import IntegerParameter, TDbsqlParameter
1718
from databricks.sql.thrift_api.TCLIService import ttypes
@@ -408,7 +409,7 @@ def test_command_execution_advanced(
408409
async_op=False,
409410
enforce_embedded_schema_correctness=False,
410411
)
411-
assert "Command test-statement-123 failed" in str(excinfo.value)
412+
assert "Command failed" in str(excinfo.value)
412413

413414
# Test missing statement ID
414415
mock_http_client.reset_mock()
@@ -530,18 +531,18 @@ def test_check_command_state(self, sea_client, sea_command_id):
530531
"""Test _check_command_not_in_failed_or_closed_state method."""
531532
# Test with RUNNING state (should not raise)
532533
sea_client._check_command_not_in_failed_or_closed_state(
533-
CommandState.RUNNING, sea_command_id
534+
StatementStatus(state=CommandState.RUNNING), sea_command_id
534535
)
535536

536537
# Test with SUCCEEDED state (should not raise)
537538
sea_client._check_command_not_in_failed_or_closed_state(
538-
CommandState.SUCCEEDED, sea_command_id
539+
StatementStatus(state=CommandState.SUCCEEDED), sea_command_id
539540
)
540541

541542
# Test with CLOSED state (should raise DatabaseError)
542543
with pytest.raises(DatabaseError) as excinfo:
543544
sea_client._check_command_not_in_failed_or_closed_state(
544-
CommandState.CLOSED, sea_command_id
545+
StatementStatus(state=CommandState.CLOSED), sea_command_id
545546
)
546547
assert "Command test-statement-123 unexpectedly closed server side" in str(
547548
excinfo.value
@@ -550,9 +551,13 @@ def test_check_command_state(self, sea_client, sea_command_id):
550551
# Test with FAILED state (should raise ServerOperationError)
551552
with pytest.raises(ServerOperationError) as excinfo:
552553
sea_client._check_command_not_in_failed_or_closed_state(
553-
CommandState.FAILED, sea_command_id
554+
StatementStatus(
555+
state=CommandState.FAILED,
556+
error=ServiceError(message="Test error", error_code="TEST_ERROR"),
557+
),
558+
sea_command_id,
554559
)
555-
assert "Command test-statement-123 failed" in str(excinfo.value)
560+
assert "Command failed" in str(excinfo.value)
556561

557562
def test_utility_methods(self, sea_client):
558563
"""Test utility methods."""

0 commit comments

Comments
 (0)