|
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 |
@@ -527,6 +528,11 @@ def __str__(self): |
527 | 528 | return "Operation not supported for buckets with source replication" |
528 | 529 |
|
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 | + |
530 | 536 | @trace_call(logger) |
531 | 537 | def interpret_b2_error( |
532 | 538 | status: int, |
@@ -565,25 +571,39 @@ def interpret_b2_error( |
565 | 571 | return PartSha1Mismatch(post_params.get('fileId')) |
566 | 572 | elif status == 400 and code == "bad_bucket_id": |
567 | 573 | return BucketIdNotFound(post_params.get('bucketId')) |
568 | | - elif status == 400 and code in ('bad_request', 'auth_token_limit', 'source_too_large'): |
569 | | - # 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": |
570 | 575 | matcher = UPLOAD_TOKEN_USED_CONCURRENTLY_ERROR_MESSAGE_RE.match(message) |
571 | | - if matcher is not None: |
572 | | - token = matcher.group('token') |
573 | | - return UploadTokenUsedConcurrently(token) |
574 | | - |
575 | | - # 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": |
576 | 580 | matcher = COPY_SOURCE_TOO_BIG_ERROR_MESSAGE_RE.match(message) |
577 | | - if matcher is not None: |
578 | | - size = int(matcher.group('size')) |
579 | | - return CopySourceTooBig(size) |
| 581 | + assert matcher is not None, f"unexpected error message: {message}" |
| 582 | + size = int(matcher.group('size')) |
| 583 | + return CopySourceTooBig(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 'disabling_file_lock_not_allowed' # TODO: cleanup after 2022-09-22 |
| 601 | + if message == 'Turning on file lock for a restricted bucket is not allowed.': |
| 602 | + return EnablingFileLockOnRestrictedBucket() |
580 | 603 |
|
581 | 604 | return BadRequest(message, code) |
582 | | - elif status == 400 and code == 'disabling_file_lock_not_allowed': |
583 | | - raise DisablingFileLockNotSupported() |
584 | | - elif status == 400 and code == 'source_replication_conflict': |
585 | | - raise SourceReplicationConflict() |
586 | 605 | elif status == 400: |
| 606 | + warnings.warn(f"bad request exception with an unknown `code`. message={message}, code={code}") |
587 | 607 | return BadRequest(message, code) |
588 | 608 | elif status == 401 and code in ("bad_auth_token", "expired_auth_token"): |
589 | 609 | return InvalidAuthToken(message, code) |
|
0 commit comments