|
12 | 12 |
|
13 | 13 | import logging |
14 | 14 | import re |
| 15 | +import warnings |
15 | 16 | from typing import Any, Dict, Optional |
16 | 17 |
|
17 | 18 | from .utils import camelcase_to_underscore, trace_call |
@@ -517,6 +518,21 @@ class CopyArgumentsMismatch(InvalidUserInput): |
517 | 518 | pass |
518 | 519 |
|
519 | 520 |
|
| 521 | +class DisablingFileLockNotSupported(B2Error): |
| 522 | + def __str__(self): |
| 523 | + return "Disabling file lock is not supported" |
| 524 | + |
| 525 | + |
| 526 | +class SourceReplicationConflict(B2Error): |
| 527 | + def __str__(self): |
| 528 | + return "Operation not supported for buckets with source replication" |
| 529 | + |
| 530 | + |
| 531 | +class EnablingFileLockOnRestrictedBucket(B2Error): |
| 532 | + def __str__(self): |
| 533 | + return "Turning on file lock for a restricted bucket is not allowed" |
| 534 | + |
| 535 | + |
520 | 536 | @trace_call(logger) |
521 | 537 | def interpret_b2_error( |
522 | 538 | status: int, |
@@ -555,21 +571,41 @@ def interpret_b2_error( |
555 | 571 | return PartSha1Mismatch(post_params.get('fileId')) |
556 | 572 | elif status == 400 and code == "bad_bucket_id": |
557 | 573 | return BucketIdNotFound(post_params.get('bucketId')) |
558 | | - elif status == 400 and code in ('bad_request', 'auth_token_limit', 'source_too_large'): |
559 | | - # it's "bad_request" on 2022-03-29, but will become 'auth_token_limit' in 2022-04 # TODO: cleanup after 2022-05-01 |
| 574 | + elif status == 400 and code == "auth_token_limit": |
560 | 575 | matcher = UPLOAD_TOKEN_USED_CONCURRENTLY_ERROR_MESSAGE_RE.match(message) |
561 | | - if matcher is not None: |
562 | | - token = matcher.group('token') |
563 | | - return UploadTokenUsedConcurrently(token) |
564 | | - |
565 | | - # it's "bad_request" on 2022-03-29, but will become 'source_too_large' in 2022-04 # TODO: cleanup after 2022-05-01 |
| 576 | + assert matcher is not None, f"unexpected error message: {message}" |
| 577 | + token = matcher.group('token') |
| 578 | + return UploadTokenUsedConcurrently(token) |
| 579 | + elif status == 400 and code == "source_too_large": |
566 | 580 | matcher = COPY_SOURCE_TOO_BIG_ERROR_MESSAGE_RE.match(message) |
567 | | - if matcher is not None: |
568 | | - size = int(matcher.group('size')) |
569 | | - return CopySourceTooBig(message, code, size) |
| 581 | + assert matcher is not None, f"unexpected error message: {message}" |
| 582 | + size = int(matcher.group('size')) |
| 583 | + return CopySourceTooBig(message, code, size) |
| 584 | + elif status == 400 and code == 'file_lock_conflict': |
| 585 | + return DisablingFileLockNotSupported() |
| 586 | + elif status == 400 and code == 'source_replication_conflict': |
| 587 | + return SourceReplicationConflict() |
| 588 | + elif status == 400 and code == 'restricted_bucket_conflict': |
| 589 | + return EnablingFileLockOnRestrictedBucket() |
| 590 | + elif status == 400 and code == 'bad_request': |
| 591 | + |
| 592 | + # it's "bad_request" on 2022-09-14, but will become 'disabling_file_lock_not_allowed' # TODO: cleanup after 2022-09-22 |
| 593 | + if message == 'fileLockEnabled value of false is not allowed when bucket is already file lock enabled.': |
| 594 | + return DisablingFileLockNotSupported() |
| 595 | + |
| 596 | + # it's "bad_request" on 2022-09-14, but will become 'source_replication_conflict' # TODO: cleanup after 2022-09-22 |
| 597 | + if message == 'Turning on file lock for an existing bucket having source replication configuration is not allowed.': |
| 598 | + return SourceReplicationConflict() |
| 599 | + |
| 600 | + # it's "bad_request" on 2022-09-14, but will become 'restricted_bucket_conflict' # TODO: cleanup after 2022-09-22 |
| 601 | + if message == 'Turning on file lock for a restricted bucket is not allowed.': |
| 602 | + return EnablingFileLockOnRestrictedBucket() |
570 | 603 |
|
571 | 604 | return BadRequest(message, code) |
572 | 605 | elif status == 400: |
| 606 | + warnings.warn( |
| 607 | + f"bad request exception with an unknown `code`. message={message}, code={code}" |
| 608 | + ) |
573 | 609 | return BadRequest(message, code) |
574 | 610 | elif status == 401 and code in ("bad_auth_token", "expired_auth_token"): |
575 | 611 | return InvalidAuthToken(message, code) |
|
0 commit comments