Skip to content

Commit 8efea35

Browse files
Revert "simplify error handling"
This reverts commit 3e281eb. Signed-off-by: varun-edachali-dbx <varun.edachali@databricks.com>
1 parent eb2dd79 commit 8efea35

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

src/databricks/sql/auth/thrift_http_client.py

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,21 @@ def _check_rest_response_for_error(
348348
response_data: Raw response data
349349
350350
Raises:
351-
Various exceptions based on the error type
351+
RequestError: If the response indicates an error
352352
"""
353353
if status_code >= 400:
354354
error_message = f"REST HTTP request failed with status {status_code}"
355355
error_code = None
356-
356+
357357
# Try to extract error details from JSON response
358358
if response_data:
359359
try:
360360
error_details = json.loads(response_data.decode("utf-8"))
361361
if isinstance(error_details, dict):
362362
if "message" in error_details:
363-
error_message = f"{error_message}: {error_details['message']}"
363+
error_message = (
364+
f"{error_message}: {error_details['message']}"
365+
)
364366
if "error_code" in error_details:
365367
error_code = error_details["error_code"]
366368
elif "errorCode" in error_details:
@@ -376,64 +378,68 @@ def _check_rest_response_for_error(
376378
logger.error(f"Request failed (status {status_code}): No response data")
377379

378380
from databricks.sql.exc import (
379-
RequestError,
380-
OperationalError,
381+
RequestError,
382+
OperationalError,
381383
DatabaseError,
382384
SessionAlreadyClosedError,
383385
CursorAlreadyClosedError,
384-
NonRecoverableNetworkError,
385-
UnsafeToRetryError
386386
)
387-
388-
# Map HTTP status codes to appropriate exceptions
387+
388+
# Map status codes to appropriate exceptions to match Thrift behavior
389389
if status_code == 429:
390-
# Rate limiting errors - similar to what ThriftDatabricksClient does
390+
# Rate limiting errors
391391
retry_after = None
392392
if self.headers and "Retry-After" in self.headers:
393393
retry_after = self.headers["Retry-After"]
394-
394+
395395
rate_limit_msg = f"Maximum rate has been exceeded. Please reduce the rate of requests and try again"
396396
if retry_after:
397397
rate_limit_msg += f" after {retry_after} seconds."
398398
raise RequestError(rate_limit_msg)
399-
399+
400400
elif status_code == 503:
401401
# Service unavailable errors
402-
raise OperationalError("TEMPORARILY_UNAVAILABLE: Service temporarily unavailable")
403-
402+
raise OperationalError(
403+
"TEMPORARILY_UNAVAILABLE: Service temporarily unavailable"
404+
)
405+
404406
elif status_code == 404:
405407
# Not found errors - could be session or operation already closed
406408
if error_message and "session" in error_message.lower():
407-
raise SessionAlreadyClosedError("Session was closed by a prior request")
408-
elif error_message and ("operation" in error_message.lower() or "statement" in error_message.lower()):
409-
raise CursorAlreadyClosedError("Operation was canceled by a prior request")
409+
raise SessionAlreadyClosedError(
410+
"Session was closed by a prior request"
411+
)
412+
elif error_message and (
413+
"operation" in error_message.lower()
414+
or "statement" in error_message.lower()
415+
):
416+
raise CursorAlreadyClosedError(
417+
"Operation was canceled by a prior request"
418+
)
410419
else:
411420
raise RequestError(error_message)
412-
421+
413422
elif status_code == 401:
414423
# Authentication errors
415-
raise OperationalError("Authentication failed. Please check your credentials.")
416-
424+
raise OperationalError(
425+
"Authentication failed. Please check your credentials."
426+
)
427+
417428
elif status_code == 403:
418429
# Permission errors
419-
raise OperationalError("Permission denied. You do not have access to this resource.")
420-
430+
raise OperationalError(
431+
"Permission denied. You do not have access to this resource."
432+
)
433+
421434
elif status_code == 400:
422435
# Bad request errors - often syntax errors
423436
if error_message and "syntax" in error_message.lower():
424-
raise DatabaseError(f"Syntax error in SQL statement: {error_message}")
437+
raise DatabaseError(
438+
f"Syntax error in SQL statement: {error_message}"
439+
)
425440
else:
426441
raise RequestError(error_message)
427-
428-
elif status_code == 501:
429-
# Not implemented errors
430-
raise NonRecoverableNetworkError(f"Not implemented: {error_message}")
431-
432-
elif status_code == 502 or status_code == 504:
433-
# Bad gateway or gateway timeout errors
434-
# These are considered dangerous to retry for ExecuteStatement
435-
raise UnsafeToRetryError(f"Gateway error: {error_message}")
436-
442+
437443
else:
438444
# Generic errors
439445
raise RequestError(error_message)

0 commit comments

Comments
 (0)