|
10 | 10 | import time
|
11 | 11 | from typing import Any, Dict, Optional
|
12 | 12 |
|
13 |
| -from cassandra import DriverException |
| 13 | +from cassandra import ( |
| 14 | + AlreadyExists, |
| 15 | + AuthenticationFailed, |
| 16 | + CDCWriteFailure, |
| 17 | + CoordinationFailure, |
| 18 | + FunctionFailure, |
| 19 | + InvalidRequest, |
| 20 | + OperationTimedOut, |
| 21 | + ReadFailure, |
| 22 | + ReadTimeout, |
| 23 | + Unauthorized, |
| 24 | + Unavailable, |
| 25 | + UnsupportedOperation, |
| 26 | + WriteFailure, |
| 27 | + WriteTimeout, |
| 28 | +) |
14 | 29 | from cassandra.cluster import _NOT_SET, EXEC_PROFILE_DEFAULT, Cluster, NoHostAvailable, Session
|
| 30 | +from cassandra.connection import ConnectionBusy, ConnectionShutdown, ProtocolError |
| 31 | +from cassandra.pool import NoConnectionsAvailable |
15 | 32 | from cassandra.protocol import ErrorMessage
|
16 | 33 | from cassandra.query import BatchStatement, PreparedStatement, SimpleStatement
|
17 | 34 |
|
@@ -177,13 +194,37 @@ async def execute(
|
177 | 194 | return result
|
178 | 195 |
|
179 | 196 | except (
|
180 |
| - DriverException, # Base class for all Cassandra driver exceptions |
| 197 | + # These exceptions should NOT be wrapped - pass through as-is |
| 198 | + ReadTimeout, |
| 199 | + WriteTimeout, |
| 200 | + OperationTimedOut, |
| 201 | + Unavailable, |
| 202 | + InvalidRequest, |
| 203 | + AlreadyExists, |
| 204 | + Unauthorized, |
| 205 | + AuthenticationFailed, |
181 | 206 | ErrorMessage, # Base class for protocol-level errors like SyntaxException
|
182 | 207 | NoHostAvailable, # Not a DriverException but should pass through
|
183 | 208 | ) as e:
|
184 |
| - # Re-raise Cassandra exceptions without wrapping |
| 209 | + # Re-raise these specific Cassandra exceptions without wrapping |
185 | 210 | error_type = type(e).__name__
|
186 | 211 | raise
|
| 212 | + except ( |
| 213 | + # These exceptions SHOULD be wrapped in QueryError |
| 214 | + UnsupportedOperation, |
| 215 | + ReadFailure, |
| 216 | + WriteFailure, |
| 217 | + FunctionFailure, |
| 218 | + CDCWriteFailure, |
| 219 | + CoordinationFailure, |
| 220 | + ProtocolError, |
| 221 | + ConnectionBusy, |
| 222 | + ConnectionShutdown, |
| 223 | + NoConnectionsAvailable, |
| 224 | + ) as e: |
| 225 | + # Wrap these exceptions in QueryError |
| 226 | + error_type = type(e).__name__ |
| 227 | + raise QueryError(f"Query execution failed: {str(e)}", cause=e) from e |
187 | 228 | except asyncio.TimeoutError:
|
188 | 229 | # Re-raise timeout errors without wrapping
|
189 | 230 | error_type = "TimeoutError"
|
@@ -290,12 +331,40 @@ async def execute_stream(
|
290 | 331 | return result
|
291 | 332 |
|
292 | 333 | except (
|
293 |
| - DriverException, # Base class for all Cassandra driver exceptions |
| 334 | + # These exceptions should NOT be wrapped - pass through as-is |
| 335 | + ReadTimeout, |
| 336 | + WriteTimeout, |
| 337 | + OperationTimedOut, |
| 338 | + Unavailable, |
| 339 | + InvalidRequest, |
| 340 | + AlreadyExists, |
| 341 | + Unauthorized, |
| 342 | + AuthenticationFailed, |
294 | 343 | ErrorMessage, # Base class for protocol-level errors like SyntaxException
|
295 | 344 | NoHostAvailable, # Not a DriverException but should pass through
|
296 | 345 | ) as e:
|
297 |
| - # Re-raise Cassandra exceptions without wrapping |
| 346 | + # Re-raise these specific Cassandra exceptions without wrapping |
| 347 | + error_type = type(e).__name__ |
| 348 | + raise |
| 349 | + except ( |
| 350 | + # These exceptions SHOULD be wrapped in QueryError |
| 351 | + UnsupportedOperation, |
| 352 | + ReadFailure, |
| 353 | + WriteFailure, |
| 354 | + FunctionFailure, |
| 355 | + CDCWriteFailure, |
| 356 | + CoordinationFailure, |
| 357 | + ProtocolError, |
| 358 | + ConnectionBusy, |
| 359 | + ConnectionShutdown, |
| 360 | + NoConnectionsAvailable, |
| 361 | + ) as e: |
| 362 | + # Wrap these exceptions in QueryError |
298 | 363 | error_type = type(e).__name__
|
| 364 | + raise QueryError(f"Streaming query execution failed: {str(e)}", cause=e) from e |
| 365 | + except asyncio.TimeoutError: |
| 366 | + # Re-raise timeout errors without wrapping |
| 367 | + error_type = "TimeoutError" |
299 | 368 | raise
|
300 | 369 | except Exception as e:
|
301 | 370 | # Only wrap non-Cassandra exceptions
|
@@ -395,9 +464,34 @@ async def prepare(
|
395 | 464 | return prepared
|
396 | 465 | except asyncio.TimeoutError:
|
397 | 466 | raise
|
398 |
| - except (DriverException, ErrorMessage, NoHostAvailable): |
399 |
| - # Re-raise Cassandra exceptions without wrapping |
| 467 | + except ( |
| 468 | + # These exceptions should NOT be wrapped - pass through as-is |
| 469 | + ReadTimeout, |
| 470 | + WriteTimeout, |
| 471 | + OperationTimedOut, |
| 472 | + Unavailable, |
| 473 | + InvalidRequest, |
| 474 | + AlreadyExists, |
| 475 | + ErrorMessage, # Base class for protocol-level errors like SyntaxException |
| 476 | + NoHostAvailable, # Not a DriverException but should pass through |
| 477 | + ): |
| 478 | + # Re-raise these specific Cassandra exceptions without wrapping |
400 | 479 | raise
|
| 480 | + except ( |
| 481 | + # These exceptions SHOULD be wrapped in QueryError |
| 482 | + UnsupportedOperation, |
| 483 | + ReadFailure, |
| 484 | + WriteFailure, |
| 485 | + FunctionFailure, |
| 486 | + CDCWriteFailure, |
| 487 | + CoordinationFailure, |
| 488 | + ProtocolError, |
| 489 | + ConnectionBusy, |
| 490 | + ConnectionShutdown, |
| 491 | + NoConnectionsAvailable, |
| 492 | + ) as e: |
| 493 | + # Wrap these exceptions in QueryError |
| 494 | + raise QueryError(f"Statement preparation failed: {str(e)}", cause=e) from e |
401 | 495 | except Exception as e:
|
402 | 496 | raise QueryError(f"Statement preparation failed: {str(e)}") from e
|
403 | 497 |
|
|
0 commit comments