Skip to content

Commit

Permalink
fix: Change exception type for missing location header
Browse files Browse the repository at this point in the history
  • Loading branch information
congminh1254 committed May 10, 2024
1 parent 8e0d640 commit de36a4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
12 changes: 10 additions & 2 deletions boxsdk/object/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
from datetime import datetime
from typing import TYPE_CHECKING, Optional, Tuple, Union, IO, Iterable, List, Any
from boxsdk.exception import BoxException
from boxsdk.exception import BoxAPIException

from boxsdk.util.datetime_formatter import normalize_date_to_rfc3339_format
from .item import Item
Expand Down Expand Up @@ -178,8 +178,16 @@ def get_download_url(self, file_version: Optional['FileVersion'] = None) -> str:
expect_json_response=False,
allow_redirects=False,
)
network_response = box_response.network_response
if 'location' not in box_response.headers:
raise BoxException('Download URL is not present in the response.')
raise BoxAPIException(
status=network_response.status_code,
headers=network_response.headers,
message='Download URL is not present in the response.',
url=url,
method='GET',
network_response=network_response,
)

return box_response.headers['location']

Expand Down
4 changes: 2 additions & 2 deletions test/unit/object/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ def test_get_download_url(test_file, mock_box_session):
def test_get_download_url_failed(test_file, mock_box_session):
expected_url = f'{API.BASE_API_URL}/files/{test_file.object_id}/content'
mock_box_session.get.return_value.headers = {}
with pytest.raises(BoxException) as exc_info:
with pytest.raises(BoxAPIException) as exc_info:
test_file.get_download_url()
assert exc_info.value.args[0] == 'Download URL is not present in the response.'
assert exc_info.value.message == 'Download URL is not present in the response.'
mock_box_session.get.assert_called_once_with(
expected_url,
params=None,
Expand Down

0 comments on commit de36a4f

Please sign in to comment.