|
8 | 8 |
|
9 | 9 | from databricks.sdk import errors |
10 | 10 | from databricks.sdk.errors import details |
| 11 | +from google.rpc import status_pb2 |
11 | 12 |
|
12 | 13 |
|
13 | 14 | def fake_response( |
@@ -415,3 +416,93 @@ def test_debug_headers_enabled_shows_headers(): |
415 | 416 | assert "debug-token-12345" in error_message |
416 | 417 | assert "X-Databricks-Azure-SP-Management-Token" in error_message |
417 | 418 | assert "debug-azure-token-67890" in error_message |
| 419 | + |
| 420 | +def test_protobuf_error_deserializer_valid_protobuf(): |
| 421 | + # Create a valid protobuf Status message |
| 422 | + status = status_pb2.Status() |
| 423 | + status.code = 3 # INVALID_ARGUMENT |
| 424 | + status.message = "Invalid parameter provided" |
| 425 | + serialized_status = status.SerializeToString() |
| 426 | + |
| 427 | + resp = fake_raw_response( |
| 428 | + method="POST", |
| 429 | + status_code=400, |
| 430 | + response_body=serialized_status, |
| 431 | + ) |
| 432 | + |
| 433 | + parser = errors._Parser() |
| 434 | + error = parser.get_api_error(resp) |
| 435 | + |
| 436 | + assert isinstance(error, errors.BadRequest) |
| 437 | + assert str(error) == "Invalid parameter provided" |
| 438 | + |
| 439 | + |
| 440 | +def test_protobuf_error_deserializer_invalid_protobuf(): |
| 441 | + # Create a response with invalid protobuf data that should fall through to other parsers |
| 442 | + resp = fake_raw_response( |
| 443 | + method="POST", |
| 444 | + status_code=400, |
| 445 | + response_body=b"\x00\x01\x02\x03\x04\x05", # Invalid protobuf |
| 446 | + ) |
| 447 | + |
| 448 | + parser = errors._Parser() |
| 449 | + error = parser.get_api_error(resp) |
| 450 | + |
| 451 | + # Should fall back to the generic error handler |
| 452 | + assert isinstance(error, errors.BadRequest) |
| 453 | + assert "unable to parse response" in str(error) |
| 454 | + |
| 455 | + |
| 456 | +def test_protobuf_error_deserializer_empty_message(): |
| 457 | + # Create a protobuf Status message with empty message |
| 458 | + status = status_pb2.Status() |
| 459 | + status.code = 5 # NOT_FOUND |
| 460 | + status.message = "" |
| 461 | + serialized_status = status.SerializeToString() |
| 462 | + |
| 463 | + resp = fake_raw_response( |
| 464 | + method="GET", |
| 465 | + status_code=404, |
| 466 | + response_body=serialized_status, |
| 467 | + ) |
| 468 | + |
| 469 | + parser = errors._Parser() |
| 470 | + error = parser.get_api_error(resp) |
| 471 | + |
| 472 | + assert isinstance(error, errors.NotFound) |
| 473 | + assert str(error) == "None" |
| 474 | + |
| 475 | + |
| 476 | +def test_protobuf_error_deserializer_with_details(): |
| 477 | + # Create a protobuf Status message with details |
| 478 | + status = status_pb2.Status() |
| 479 | + status.code = 9 # FAILED_PRECONDITION |
| 480 | + status.message = "Resource is in an invalid state" |
| 481 | + serialized_status = status.SerializeToString() |
| 482 | + |
| 483 | + resp = fake_raw_response( |
| 484 | + method="POST", |
| 485 | + status_code=400, |
| 486 | + response_body=serialized_status, |
| 487 | + ) |
| 488 | + |
| 489 | + parser = errors._Parser() |
| 490 | + error = parser.get_api_error(resp) |
| 491 | + |
| 492 | + assert isinstance(error, errors.BadRequest) |
| 493 | + assert str(error) == "Resource is in an invalid state" |
| 494 | + |
| 495 | + |
| 496 | +def test_protobuf_error_deserializer_priority(): |
| 497 | + resp = fake_valid_response( |
| 498 | + method="POST", |
| 499 | + status_code=400, |
| 500 | + error_code="INVALID_REQUEST", |
| 501 | + message="Invalid request body", |
| 502 | + ) |
| 503 | + |
| 504 | + parser = errors._Parser() |
| 505 | + error = parser.get_api_error(resp) |
| 506 | + |
| 507 | + assert isinstance(error, errors.BadRequest) |
| 508 | + assert str(error) == "Invalid request body" |
0 commit comments