1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
|
######################################################################
#
# File: b2sdk/_internal/exception.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations
import logging
import re
import typing
import warnings
from abc import ABCMeta
from typing import Any
from .utils import camelcase_to_underscore, trace_call
UPLOAD_TOKEN_USED_CONCURRENTLY_ERROR_MESSAGE_RE = re.compile(
r'^more than one upload using auth token (?P<token>[^)]+)$'
)
COPY_SOURCE_TOO_BIG_ERROR_MESSAGE_RE = re.compile(r'^Copy source too big: (?P<size>[\d]+)$')
logger = logging.getLogger(__name__)
class B2Error(Exception, metaclass=ABCMeta):
def __init__(self, *args, **kwargs):
"""
Python 2 does not like it when you pass unicode as the message
in an exception. We like to use file names in exception messages.
To avoid problems, if the message has any non-ascii characters in
it, they are replaced with backslash-uNNNN.
https://pythonhosted.org/kitchen/unicode-frustrations.html#frustration-5-exceptions
"""
# If the exception is caused by a b2 server response,
# the server MAY have included instructions to pause the thread before issuing any more requests
self.retry_after_seconds = None
super().__init__(*args, **kwargs)
@property
def prefix(self):
"""
Nice, auto-generated error message prefix.
>>> B2SimpleError().prefix
'Simple error'
>>> AlreadyFailed().prefix
'Already failed'
"""
prefix = self.__class__.__name__
if prefix.startswith('B2'):
prefix = prefix[2:]
prefix = camelcase_to_underscore(prefix).replace('_', ' ')
return prefix[0].upper() + prefix[1:]
def should_retry_http(self):
"""
Return true if this is an error that can cause an HTTP
call to be retried.
"""
return False
def should_retry_upload(self):
"""
Return true if this is an error that should tell the upload
code to get a new upload URL and try the upload again.
"""
return False
class InvalidUserInput(B2Error):
pass
class B2SimpleError(B2Error, metaclass=ABCMeta):
"""
A B2Error with a message prefix.
"""
def __str__(self):
return f'{self.prefix}: {super().__str__()}'
class NotAllowedByAppKeyError(B2SimpleError, metaclass=ABCMeta):
"""
Base class for errors caused by restrictions on an application key.
"""
class TransientErrorMixin(metaclass=ABCMeta):
def should_retry_http(self):
return True
def should_retry_upload(self):
return True
class AlreadyFailed(B2SimpleError):
pass
class BadDateFormat(B2SimpleError):
prefix = 'Date from server'
class BadFileInfo(B2SimpleError):
pass
class BadJson(B2SimpleError):
prefix = 'Bad request'
class BadUploadUrl(B2SimpleError):
def should_retry_upload(self):
return True
class BrokenPipe(B2Error):
def __str__(self):
return 'Broken pipe: unable to send entire request'
def should_retry_upload(self):
return True
class CapabilityNotAllowed(NotAllowedByAppKeyError):
pass
class ChecksumMismatch(TransientErrorMixin, B2Error):
def __init__(self, checksum_type, expected, actual):
super().__init__()
self.checksum_type = checksum_type
self.expected = expected
self.actual = actual
def __str__(self):
return f'{self.checksum_type} checksum mismatch -- bad data'
class B2HttpCallbackException(B2SimpleError):
pass
class B2HttpCallbackPostRequestException(B2HttpCallbackException):
pass
class B2HttpCallbackPreRequestException(B2HttpCallbackException):
pass
class BucketNotAllowed(NotAllowedByAppKeyError):
pass
class ClockSkew(B2HttpCallbackPostRequestException):
"""
The clock on the server differs from the local clock by too much.
"""
def __init__(self, clock_skew_seconds):
"""
:param int clock_skew_seconds: The difference: local_clock - server_clock
"""
super().__init__()
self.clock_skew_seconds = clock_skew_seconds
def __str__(self):
if self.clock_skew_seconds < 0:
return 'ClockSkew: local clock is %d seconds behind server' % (
-self.clock_skew_seconds,
)
else:
return 'ClockSkew; local clock is %d seconds ahead of server' % (
self.clock_skew_seconds,
)
class Conflict(B2SimpleError):
pass
class ConnectionReset(B2Error):
def __str__(self):
return 'Connection reset'
def should_retry_upload(self):
return True
class B2ConnectionError(TransientErrorMixin, B2SimpleError):
pass
class B2RequestTimeout(TransientErrorMixin, B2SimpleError):
pass
class B2RequestTimeoutDuringUpload(B2RequestTimeout):
# if a timeout is hit during upload, it is not guaranteed that the the server has released the upload token lock already, so we'll use a new token
def should_retry_http(self):
return False
class DestFileNewer(B2Error):
def __init__(self, dest_path, source_path, dest_prefix, source_prefix):
super().__init__()
self.dest_path = dest_path
self.source_path = source_path
self.dest_prefix = dest_prefix
self.source_prefix = source_prefix
def __str__(self):
return f'source file is older than destination: {self.source_prefix}{self.source_path.relative_path} with a time of {self.source_path.mod_time} cannot be synced to {self.dest_prefix}{self.dest_path.relative_path} with a time of {self.dest_path.mod_time}, unless a valid newer_file_mode is provided'
def should_retry_http(self):
return True
class DuplicateBucketName(B2SimpleError):
prefix = 'Bucket name is already in use'
class ResourceNotFound(B2SimpleError):
prefix = 'No such file, bucket, or endpoint'
class FileOrBucketNotFound(ResourceNotFound):
def __init__(self, bucket_name=None, file_id_or_name=None):
super().__init__()
self.bucket_name = bucket_name
self.file_id_or_name = file_id_or_name
def __str__(self):
file_str = ('file [%s]' % self.file_id_or_name) if self.file_id_or_name else 'a file'
bucket_str = ('bucket [%s]' % self.bucket_name) if self.bucket_name else 'a bucket'
return f'Could not find {file_str} within {bucket_str}'
class BucketIdNotFound(ResourceNotFound):
def __init__(self, bucket_id):
self.bucket_id = bucket_id
def __str__(self):
return f'Bucket with id={self.bucket_id} not found'
class FileAlreadyHidden(B2SimpleError):
pass
class FileNotHidden(B2SimpleError):
prefix = 'File not hidden'
class FileDeleted(B2SimpleError):
prefix = 'File deleted'
class UnexpectedFileVersionAction(B2SimpleError):
prefix = 'Unexpected file version action returned by the server'
class FileNameNotAllowed(NotAllowedByAppKeyError):
pass
class FileNotPresent(FileOrBucketNotFound):
def __str__(self): # overridden to retain message across prev versions
return 'File not present%s' % (': ' + self.file_id_or_name if self.file_id_or_name else '')
class UnusableFileName(B2SimpleError):
"""
Raise when a filename doesn't meet the rules.
Could possibly use InvalidUploadSource, but this is intended for the filename on the
server, which could differ. https://www.backblaze.com/b2/docs/files.html.
"""
pass
class InvalidMetadataDirective(B2Error):
pass
class SSECKeyIdMismatchInCopy(InvalidMetadataDirective):
pass
class InvalidRange(B2Error):
def __init__(self, content_length, range_):
super().__init__()
self.content_length = content_length
self.range_ = range_
def __str__(self):
return (
'A range of %d-%d was requested (size of %d), but cloud could only serve %d of that'
% (
self.range_[0],
self.range_[1],
self.range_[1] - self.range_[0] + 1,
self.content_length,
)
)
class InvalidUploadSource(B2SimpleError):
pass
class BadRequest(B2Error):
def __init__(self, message, code):
super().__init__()
self.message = message
self.code = code
def __str__(self):
return f'{self.message} ({self.code})'
class CopySourceTooBig(BadRequest):
def __init__(self, message, code, size: int):
super().__init__(message, code)
self.size = size
class Unauthorized(B2Error):
def __init__(self, message, code):
super().__init__()
self.message = message
self.code = code
def __str__(self):
return f'{self.message} ({self.code})'
def should_retry_upload(self):
return True
class EmailNotVerified(Unauthorized):
def should_retry_upload(self):
return False
class NoPaymentHistory(Unauthorized):
def should_retry_upload(self):
return False
class InvalidAuthToken(Unauthorized):
"""
Specific type of Unauthorized that means the auth token is invalid.
This is not the case where the auth token is valid, but does not
allow access.
"""
def __init__(self, message, code):
super().__init__('Invalid authorization token. Server said: ' + message, code)
class RestrictedBucket(B2Error):
def __init__(self, bucket_name):
super().__init__()
self.bucket_name = bucket_name
def __str__(self):
return 'Application key is restricted to bucket: %s' % self.bucket_name
class RestrictedBucketMissing(RestrictedBucket):
def __init__(self):
super().__init__('')
def __str__(self):
return "Application key is restricted to a bucket that doesn't exist"
class MaxFileSizeExceeded(B2Error):
def __init__(self, size, max_allowed_size):
super().__init__()
self.size = size
self.max_allowed_size = max_allowed_size
def __str__(self):
return f'Allowed file size of exceeded: {self.size} > {self.max_allowed_size}'
class MaxRetriesExceeded(B2Error):
def __init__(self, limit, exception_info_list):
super().__init__()
self.limit = limit
self.exception_info_list = exception_info_list
def __str__(self):
exceptions = '\n'.join(str(wrapped_error) for wrapped_error in self.exception_info_list)
return f'FAILED to upload after {self.limit} tries. Encountered exceptions: {exceptions}'
class MissingPart(B2SimpleError):
prefix = 'Part number has not been uploaded'
class NonExistentBucket(FileOrBucketNotFound):
def __str__(self): # overridden to retain message across prev versions
return 'No such bucket%s' % (': ' + self.bucket_name if self.bucket_name else '')
class FileSha1Mismatch(B2SimpleError):
prefix = 'Upload file SHA1 mismatch'
class PartSha1Mismatch(B2Error):
def __init__(self, key):
super().__init__()
self.key = key
def __str__(self):
return f'Part number {self.key} has wrong SHA1'
class ServiceError(TransientErrorMixin, B2Error):
"""
Used for HTTP status codes 500 through 599.
"""
class CapExceeded(B2Error):
def __str__(self):
return 'Cap exceeded.'
class StorageCapExceeded(CapExceeded):
def __str__(self):
return 'Cannot upload or copy files, storage cap exceeded.'
class TransactionCapExceeded(CapExceeded):
def __str__(self):
return 'Cannot perform the operation, transaction cap exceeded.'
class TooManyRequests(B2Error):
def __init__(self, retry_after_seconds=None):
super().__init__()
self.retry_after_seconds = retry_after_seconds
def __str__(self):
return 'Too many requests'
def should_retry_http(self):
return True
class TruncatedOutput(TransientErrorMixin, B2Error):
def __init__(self, bytes_read, file_size):
super().__init__()
self.bytes_read = bytes_read
self.file_size = file_size
def __str__(self):
return 'only %d of %d bytes read' % (
self.bytes_read,
self.file_size,
)
class UnexpectedCloudBehaviour(B2SimpleError):
pass
class UnknownError(B2SimpleError):
pass
class UnknownHost(B2Error):
def __str__(self):
return 'unknown host'
class UnrecognizedBucketType(B2Error):
pass
class UnsatisfiableRange(B2Error):
def __str__(self):
return 'The range in the request is outside the size of the file'
class UploadTokenUsedConcurrently(B2Error):
def __init__(self, token):
super().__init__()
self.token = token
def __str__(self):
return f'More than one concurrent upload using auth token {self.token}'
class AccessDenied(B2Error):
def __str__(self):
return 'This call with these parameters is not allowed for this auth token'
class SSECKeyError(AccessDenied):
def __str__(self):
return 'Wrong or no SSE-C key provided when reading a file.'
class RetentionWriteError(AccessDenied):
def __str__(self):
return (
"Auth token not authorized to write retention or file already in 'compliance' mode or "
'bypassGovernance=true parameter missing'
)
class WrongEncryptionModeForBucketDefault(InvalidUserInput):
def __init__(self, encryption_mode):
super().__init__()
self.encryption_mode = encryption_mode
def __str__(self):
return f'{self.encryption_mode} cannot be used as default for a bucket.'
class CopyArgumentsMismatch(InvalidUserInput):
pass
class DisablingFileLockNotSupported(B2Error):
def __str__(self):
return 'Disabling file lock is not supported'
class SourceReplicationConflict(B2Error):
def __str__(self):
return 'Operation not supported for buckets with source replication'
class EnablingFileLockOnRestrictedBucket(B2Error):
def __str__(self):
return 'Turning on file lock for a restricted bucket is not allowed'
class InvalidJsonResponse(B2SimpleError):
UP_TO_BYTES_COUNT = 200
def __init__(self, content: bytes):
self.content = content
message = self.content[: self.UP_TO_BYTES_COUNT].decode('utf-8', errors='replace')
if len(self.content) > self.UP_TO_BYTES_COUNT:
message += '...'
super().__init__(message)
class PotentialS3EndpointPassedAsRealm(InvalidJsonResponse):
pass
class DestinationError(B2Error):
pass
class DestinationDirectoryError(DestinationError):
pass
class DestinationDirectoryDoesntExist(DestinationDirectoryError):
pass
class DestinationParentIsNotADirectory(DestinationDirectoryError):
pass
class DestinationIsADirectory(DestinationDirectoryError):
pass
class DestinationDirectoryDoesntAllowOperation(DestinationDirectoryError):
pass
class EventTypeError(BadRequest):
pass
class EventTypeCategoriesError(EventTypeError):
pass
class EventTypeOverlapError(EventTypeError):
pass
class EventTypesEmptyError(EventTypeError):
pass
class EventTypeInvalidError(EventTypeError):
pass
def _event_type_invalid_error(code: str, message: str, **_) -> B2Error:
from b2sdk._internal.raw_api import EVENT_TYPE
valid_types = sorted(typing.get_args(EVENT_TYPE))
return EventTypeInvalidError(
f'Event Type error: {message!r}. Valid types: {sorted(valid_types)!r}', code
)
_error_handlers: dict[tuple[int, str | None], typing.Callable] = {
(400, 'event_type_categories'): lambda code, message, **_: EventTypeCategoriesError(
message, code
),
(400, 'event_type_overlap'): lambda code, message, **_: EventTypeOverlapError(message, code),
(400, 'event_types_empty'): lambda code, message, **_: EventTypesEmptyError(message, code),
(400, 'event_type_invalid'): _event_type_invalid_error,
(401, 'email_not_verified'): lambda code, message, **_: EmailNotVerified(message, code),
(401, 'no_payment_history'): lambda code, message, **_: NoPaymentHistory(message, code),
}
@trace_call(logger)
def interpret_b2_error(
status: int,
code: str | None,
message: str | None,
response_headers: dict[str, Any],
post_params: dict[str, Any] | None = None,
) -> B2Error:
post_params = post_params or {}
handler = _error_handlers.get((status, code))
if handler:
error = handler(
status=status,
code=code,
message=message,
response_headers=response_headers,
post_params=post_params,
)
if error:
return error
if status == 400 and code == 'already_hidden':
return FileAlreadyHidden(post_params.get('fileName'))
elif status == 400 and code == 'bad_json':
return BadJson(message)
elif (status == 400 and code in ('no_such_file', 'file_not_present')) or (
status == 404 and code == 'not_found'
):
# hide_file returns 400 and "no_such_file"
# delete_file_version returns 400 and "file_not_present"
# get_file_info returns 404 and "not_found"
# download_file_by_name/download_file_by_id return 404 and "not_found"
# but don't have post_params
return FileNotPresent(
file_id_or_name=post_params.get('fileId') or post_params.get('fileName')
)
elif status == 404:
# often times backblaze will return cryptic error messages on invalid URLs.
# We should ideally only reach that case on programming error or outdated
# sdk versions, but to prevent user confusion we omit the message param
return ResourceNotFound()
elif status == 400 and code == 'duplicate_bucket_name':
return DuplicateBucketName(post_params.get('bucketName'))
elif status == 400 and code == 'missing_part':
return MissingPart(post_params.get('fileId'))
elif status == 400 and code == 'part_sha1_mismatch':
return PartSha1Mismatch(post_params.get('fileId'))
elif status == 400 and code == 'bad_bucket_id':
return BucketIdNotFound(post_params.get('bucketId'))
elif status == 400 and code == 'auth_token_limit':
matcher = UPLOAD_TOKEN_USED_CONCURRENTLY_ERROR_MESSAGE_RE.match(message)
assert matcher is not None, f'unexpected error message: {message}'
token = matcher.group('token')
return UploadTokenUsedConcurrently(token)
elif status == 400 and code == 'source_too_large':
matcher = COPY_SOURCE_TOO_BIG_ERROR_MESSAGE_RE.match(message)
assert matcher is not None, f'unexpected error message: {message}'
size = int(matcher.group('size'))
return CopySourceTooBig(message, code, size)
elif status == 400 and code == 'file_lock_conflict':
return DisablingFileLockNotSupported()
elif status == 400 and code == 'source_replication_conflict':
return SourceReplicationConflict()
elif status == 400 and code == 'restricted_bucket_conflict':
return EnablingFileLockOnRestrictedBucket()
elif status == 400 and code == 'bad_request':
# it's "bad_request" on 2022-09-14, but will become 'disabling_file_lock_not_allowed' # TODO: cleanup after 2022-09-22
if (
message
== 'fileLockEnabled value of false is not allowed when bucket is already file lock enabled.'
):
return DisablingFileLockNotSupported()
# it's "bad_request" on 2022-09-14, but will become 'source_replication_conflict' # TODO: cleanup after 2022-09-22
if (
message
== 'Turning on file lock for an existing bucket having source replication configuration is not allowed.'
):
return SourceReplicationConflict()
# it's "bad_request" on 2022-09-14, but will become 'restricted_bucket_conflict' # TODO: cleanup after 2022-09-22
if message == 'Turning on file lock for a restricted bucket is not allowed.':
return EnablingFileLockOnRestrictedBucket()
return BadRequest(message, code)
elif status == 400:
warnings.warn(
f'bad request exception with an unknown `code`. message={message}, code={code}'
)
return BadRequest(message, code)
elif status == 401 and code in ('bad_auth_token', 'expired_auth_token'):
return InvalidAuthToken(message, code)
elif status == 401:
return Unauthorized(message, code)
elif status == 403 and code == 'storage_cap_exceeded':
return StorageCapExceeded()
elif status == 403 and code == 'transaction_cap_exceeded':
return TransactionCapExceeded()
elif status == 403 and code == 'access_denied':
return AccessDenied()
elif status == 409:
return Conflict()
elif status == 416 and code == 'range_not_satisfiable':
return UnsatisfiableRange()
elif status == 429:
return TooManyRequests(retry_after_seconds=response_headers.get('retry-after'))
elif 500 <= status < 600:
return ServiceError('%d %s %s' % (status, code, message))
return UnknownError('%d %s %s' % (status, code, message))
|