From de36a4fa076d2e6e996db25bab431722adc2760e Mon Sep 17 00:00:00 2001 From: Minh Nguyen Cong Date: Fri, 10 May 2024 13:08:41 +0200 Subject: [PATCH] fix: Change exception type for missing location header --- boxsdk/object/file.py | 12 ++++++++++-- test/unit/object/test_file.py | 4 ++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/boxsdk/object/file.py b/boxsdk/object/file.py index 17eda2cb..b63c6662 100644 --- a/boxsdk/object/file.py +++ b/boxsdk/object/file.py @@ -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 @@ -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'] diff --git a/test/unit/object/test_file.py b/test/unit/object/test_file.py index 4449e496..f928a1ab 100644 --- a/test/unit/object/test_file.py +++ b/test/unit/object/test_file.py @@ -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,