Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins): raise error when http download order fails #1338

Merged
merged 3 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(server): don't crash if exception has status_code None
  • Loading branch information
amarandon committed Oct 10, 2024
commit f2bfdcc0496ace601f40a27bb4d39d629dde357c
5 changes: 4 additions & 1 deletion eodag/rest/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ async def eodag_errors_handler(request: Request, exc: Exception) -> ORJSONRespon
if not isinstance(exc, EodagError):
return starlette_exception_handler(request, exc)

code = EODAG_DEFAULT_STATUS_CODES.get(type(exc), getattr(exc, "status_code", 500))
exception_status_code = getattr(exc, "status_code", None)
default_status_code = exception_status_code or 500
code = EODAG_DEFAULT_STATUS_CODES.get(type(exc), default_status_code)

detail = f"{type(exc).__name__}: {str(exc)}"

if type(exc) in (MisconfiguredError, AuthenticationError, TimeOutError):
Expand Down
31 changes: 30 additions & 1 deletion tests/units/test_http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ def _request_valid_raw(
post_data: Optional[Any] = None,
search_call_count: Optional[int] = None,
search_result: SearchResult = None,
expected_status_code: int = 200,
) -> httpx.Response:
if search_result:
mock_search.return_value = search_result
Expand Down Expand Up @@ -354,7 +355,7 @@ def _request_valid_raw(
elif expected_search_kwargs is not None:
mock_search.assert_called_once_with(**expected_search_kwargs)

self.assertEqual(200, response.status_code, response.text)
self.assertEqual(expected_status_code, response.status_code, response.text)

return response

Expand Down Expand Up @@ -1150,6 +1151,34 @@ def test_download_item_from_collection_no_stream(
expected_file
), f"File {expected_file} should have been deleted"

@mock.patch(
"eodag.plugins.authentication.base.Authentication.authenticate",
autospec=True,
)
@mock.patch(
"eodag.plugins.download.base.Download._stream_download_dict",
autospec=True,
)
def test_error_handler_supports_request_exception_with_status_code_none(
self, mock_stream_download: Mock, mock_auth: Mock
):
"""
A RequestError with a status code set to None (the default value) should not
crash the server. This test ensures that it doesn't crash the server and that a
500 status code is returned to the user.
"""
# Make _stream_download_dict reaise an exception object with a status_code
# attribute set to None
exception = RequestError()
exception.status_code = None
mock_stream_download.side_effect = exception

response = self._request_valid_raw(
f"collections/{self.tested_product_type}/items/foo/download?provider=peps",
expected_status_code=500,
)
assert "RequestError" in response.text
amarandon marked this conversation as resolved.
Show resolved Hide resolved

def test_root(self):
"""Request to / should return a valid response"""
resp_json = self._request_valid("", check_links=False)
Expand Down
  NODES
COMMUNITY 2
Note 1
Project 2
USERS 1