Skip to content

Commit 48a88e3

Browse files
committed
Clean up interpret_b2_errors and fix file lock exceptions
1 parent 6fe915a commit 48a88e3

File tree

1 file changed

+34
-14
lines changed

1 file changed

+34
-14
lines changed

b2sdk/exception.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import logging
1414
import re
15+
import warnings
1516
from typing import Any, Dict, Optional
1617

1718
from .utils import camelcase_to_underscore, trace_call
@@ -527,6 +528,11 @@ def __str__(self):
527528
return "Operation not supported for buckets with source replication"
528529

529530

531+
class EnablingFileLockOnRestrictedBucket(B2Error):
532+
def __str__(self):
533+
return "Turning on file lock for a restricted bucket is not allowed"
534+
535+
530536
@trace_call(logger)
531537
def interpret_b2_error(
532538
status: int,
@@ -565,25 +571,39 @@ def interpret_b2_error(
565571
return PartSha1Mismatch(post_params.get('fileId'))
566572
elif status == 400 and code == "bad_bucket_id":
567573
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":
570575
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":
576580
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()
580603

581604
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()
586605
elif status == 400:
606+
warnings.warn(f"bad request exception with an unknown `code`. message={message}, code={code}")
587607
return BadRequest(message, code)
588608
elif status == 401 and code in ("bad_auth_token", "expired_auth_token"):
589609
return InvalidAuthToken(message, code)

0 commit comments

Comments
 (0)