-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from ral-facilities/error-signalling
Search API Error Signalling/Formatting
- Loading branch information
Showing
5 changed files
with
105 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import pytest | ||
|
||
from datagateway_api.src.common.exceptions import ( | ||
BadRequestError, | ||
FilterError, | ||
MissingRecordError, | ||
SearchAPIError, | ||
) | ||
from datagateway_api.src.search_api.helpers import search_api_error_handling | ||
|
||
|
||
class TestErrorHandling: | ||
@pytest.mark.parametrize( | ||
"raised_exception, expected_exception, status_code", | ||
[ | ||
pytest.param(BadRequestError, BadRequestError, 400, id="Bad request error"), | ||
pytest.param(FilterError, FilterError, 400, id="Invalid filter"), | ||
pytest.param( | ||
MissingRecordError, MissingRecordError, 404, id="Missing record", | ||
), | ||
pytest.param(SearchAPIError, SearchAPIError, 500, id="Search API error"), | ||
pytest.param(TypeError, BadRequestError, 400, id="Type error"), | ||
pytest.param(ValueError, BadRequestError, 400, id="Value error"), | ||
pytest.param(AttributeError, BadRequestError, 400, id="Attribute error"), | ||
], | ||
) | ||
def test_valid_error_raised( | ||
self, raised_exception, expected_exception, status_code, | ||
): | ||
@search_api_error_handling | ||
def raise_exception(): | ||
raise raised_exception() | ||
|
||
try: | ||
raise_exception() | ||
except Exception as e: | ||
assert isinstance(e.args[0], dict) | ||
assert e.status_code == status_code | ||
assert list(e.args[0]["error"].keys()) == ["statusCode", "name", "message"] | ||
|
||
with pytest.raises(expected_exception): | ||
raise_exception() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters