Skip to content

Commit

Permalink
Merge pull request #494 from reef-technologies/fix_bucket_not_found_exc
Browse files Browse the repository at this point in the history
ensure b2sdk.v2 emits v2 BucketIdNotFound exc
  • Loading branch information
mjurbanski-reef authored May 8, 2024
2 parents ef702a5 + 2b20921 commit 7c07b6f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion b2sdk/v2/b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Overridden to retain old-style BadRequest exception in case of a bad bucket id
class B2Http(v3.B2Http):
@classmethod
def _translate_error(cls, fcn, post_params=None):
def _translate_errors(cls, fcn, post_params=None):
try:
return super()._translate_errors(fcn, post_params)
except v3BucketIdNotFound as e:
Expand Down
1 change: 1 addition & 0 deletions changelog.d/437.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ensure `b2sdk.v2.b2http` emits `b2sdk.v2.BucketIdNotFound` exception instead of `b2sdk._v3.BucketIdNotFound`.
32 changes: 21 additions & 11 deletions test/unit/b2http/test_b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,24 @@
from ..test_base import TestBase


class TestTranslateErrors(TestBase):
class TestTranslateErrors:
def test_ok(self):
response = MagicMock()
response.status_code = 200
actual = B2Http._translate_errors(lambda: response)
self.assertIs(response, actual)
assert response == actual

def test_partial_content(self):
response = MagicMock()
response.status_code = 206
actual = B2Http._translate_errors(lambda: response)
self.assertIs(response, actual)
assert response == actual

def test_b2_error(self):
response = MagicMock()
response.status_code = 503
response.content = b'{"status": 503, "code": "server_busy", "message": "busy"}'
with self.assertRaises(ServiceError):
with pytest.raises(ServiceError):
B2Http._translate_errors(lambda: response)

def test_broken_pipe(self):
Expand All @@ -66,7 +66,7 @@ def fcn():
)
)

with self.assertRaises(BrokenPipe):
with pytest.raises(BrokenPipe):
B2Http._translate_errors(fcn)

def test_unknown_host(self):
Expand All @@ -77,14 +77,14 @@ def fcn():
)
)

with self.assertRaises(UnknownHost):
with pytest.raises(UnknownHost):
B2Http._translate_errors(fcn)

def test_connection_error(self):
def fcn():
raise requests.ConnectionError('a message')

with self.assertRaises(B2ConnectionError):
with pytest.raises(B2ConnectionError):
B2Http._translate_errors(fcn)

def test_connection_reset(self):
Expand All @@ -94,22 +94,22 @@ class SysCallError(Exception):
def fcn():
raise SysCallError('(104, ECONNRESET)')

with self.assertRaises(ConnectionReset):
with pytest.raises(ConnectionReset):
B2Http._translate_errors(fcn)

def test_unknown_error(self):
def fcn():
raise Exception('a message')

with self.assertRaises(UnknownError):
with pytest.raises(UnknownError):
B2Http._translate_errors(fcn)

def test_too_many_requests(self):
response = MagicMock()
response.status_code = 429
response.headers = {'retry-after': 1}
response.content = b'{"status": 429, "code": "Too Many requests", "message": "retry after some time"}'
with self.assertRaises(TooManyRequests):
with pytest.raises(TooManyRequests):
B2Http._translate_errors(lambda: response)

def test_invalid_json(self):
Expand All @@ -129,9 +129,19 @@ def test_potential_s3_endpoint_passed_as_realm(self):
response.content = b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
response.url = 'https://s3.us-west-000.backblazeb2.com'

with self.assertRaises(PotentialS3EndpointPassedAsRealm):
with pytest.raises(PotentialS3EndpointPassedAsRealm):
B2Http._translate_errors(lambda: response)

@pytest.mark.apiver(to_ver=2)
def test_bucket_id_not_found(self):
from b2sdk.v2.exception import BucketIdNotFound, v3BucketIdNotFound

def fcn():
raise v3BucketIdNotFound('bucket_id')

with pytest.raises(BucketIdNotFound):
B2Http._translate_errors(fcn)


def test_b2_error__nginx_html():
"""
Expand Down

0 comments on commit 7c07b6f

Please sign in to comment.