forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ApiError message formatting; add
response
field to ApiError…
… and UnexpectedResponseError (linode#467) * Improve ApiError and UnexpectedResponseError * make format * Fix again * Remove comma from multiline string
- Loading branch information
1 parent
0139b51
commit 60622fc
Showing
7 changed files
with
269 additions
and
39 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
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,28 @@ | ||
from linode_api4.errors import ApiError | ||
|
||
|
||
def test_error_404(test_linode_client): | ||
api_exc = None | ||
|
||
try: | ||
test_linode_client.get("/invalid/endpoint") | ||
except ApiError as exc: | ||
api_exc = exc | ||
|
||
assert str(api_exc) == "GET /v4beta/invalid/endpoint: [404] Not found" | ||
|
||
|
||
def test_error_400(test_linode_client): | ||
api_exc = None | ||
|
||
try: | ||
test_linode_client.linode.instance_create( | ||
"g6-fake-plan", "us-fakeregion" | ||
) | ||
except ApiError as exc: | ||
api_exc = exc | ||
|
||
assert str(api_exc) == ( | ||
"POST /v4beta/linode/instances: [400] type: A valid plan type by that ID was not found; " | ||
"region: region is not valid" | ||
) |
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,104 @@ | ||
from types import SimpleNamespace | ||
from unittest import TestCase | ||
|
||
from linode_api4.errors import ApiError, UnexpectedResponseError | ||
|
||
|
||
class ApiErrorTest(TestCase): | ||
def test_from_response(self): | ||
mock_response = SimpleNamespace( | ||
status_code=400, | ||
json=lambda: { | ||
"errors": [ | ||
{"reason": "foo"}, | ||
{"field": "bar", "reason": "oh no"}, | ||
] | ||
}, | ||
text='{"errors": [{"reason": "foo"}, {"field": "bar", "reason": "oh no"}]}', | ||
request=SimpleNamespace( | ||
method="POST", | ||
path_url="foo/bar", | ||
), | ||
) | ||
|
||
exc = ApiError.from_response(mock_response) | ||
|
||
assert str(exc) == "POST foo/bar: [400] foo; bar: oh no" | ||
assert exc.status == 400 | ||
assert exc.json == { | ||
"errors": [{"reason": "foo"}, {"field": "bar", "reason": "oh no"}] | ||
} | ||
assert exc.response.request.method == "POST" | ||
assert exc.response.request.path_url == "foo/bar" | ||
|
||
def test_from_response_non_json_body(self): | ||
mock_response = SimpleNamespace( | ||
status_code=500, | ||
json=lambda: None, | ||
text="foobar", | ||
request=SimpleNamespace( | ||
method="POST", | ||
path_url="foo/bar", | ||
), | ||
) | ||
|
||
exc = ApiError.from_response(mock_response) | ||
|
||
assert str(exc) == "POST foo/bar: [500] foobar" | ||
assert exc.status == 500 | ||
assert exc.json is None | ||
assert exc.response.request.method == "POST" | ||
assert exc.response.request.path_url == "foo/bar" | ||
|
||
def test_from_response_empty_body(self): | ||
mock_response = SimpleNamespace( | ||
status_code=500, | ||
json=lambda: None, | ||
text=None, | ||
request=SimpleNamespace( | ||
method="POST", | ||
path_url="foo/bar", | ||
), | ||
) | ||
|
||
exc = ApiError.from_response(mock_response) | ||
|
||
assert str(exc) == "POST foo/bar: [500] N/A" | ||
assert exc.status == 500 | ||
assert exc.json is None | ||
assert exc.response.request.method == "POST" | ||
assert exc.response.request.path_url == "foo/bar" | ||
|
||
def test_from_response_no_request(self): | ||
mock_response = SimpleNamespace( | ||
status_code=500, json=lambda: None, text="foobar", request=None | ||
) | ||
|
||
exc = ApiError.from_response(mock_response) | ||
|
||
assert str(exc) == "[500] foobar" | ||
assert exc.status == 500 | ||
assert exc.json is None | ||
assert exc.response.request is None | ||
|
||
|
||
class UnexpectedResponseErrorTest(TestCase): | ||
def test_from_response(self): | ||
mock_response = SimpleNamespace( | ||
status_code=400, | ||
json=lambda: { | ||
"foo": "bar", | ||
}, | ||
request=SimpleNamespace( | ||
method="POST", | ||
path_url="foo/bar", | ||
), | ||
) | ||
|
||
exc = UnexpectedResponseError.from_response("foobar", mock_response) | ||
|
||
assert str(exc) == "foobar" | ||
assert exc.status == 400 | ||
assert exc.json == {"foo": "bar"} | ||
assert exc.response.request.method == "POST" | ||
assert exc.response.request.path_url == "foo/bar" |