Skip to content

Commit 6f8ce61

Browse files
committed
Make Exceptions proper objects with members
So users of the client don't have to specify a certain index, but can use err.status_code, for example
1 parent 0dbb8a6 commit 6f8ce61

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/openqa_client/exceptions.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
"""Custom exceptions used by openqa_client."""
1919

20+
from requests.exceptions import ConnectionError as RConnectionError
21+
2022

2123
class OpenQAClientError(Exception):
2224
"""Base class for openQA client errors."""
@@ -29,12 +31,16 @@ class ConnectionError(OpenQAClientError):
2931
requests.exceptions.ConnectionError.
3032
"""
3133

32-
pass
34+
def __init__(self, err: RConnectionError) -> None:
35+
self.err = err
3336

3437

3538
class RequestError(OpenQAClientError):
3639
"""Error raised when a request fails (after retries). 3-tuple of
3740
method, URL, and status code.
3841
"""
3942

40-
pass
43+
def __init__(self, method: str, url: str, status_code: int) -> None:
44+
self.method = method
45+
self.url = url
46+
self.status_code = status_code

tests/test_client.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,17 @@ def test_get_jobs(self, fakerequest, fakeclones, simple_config):
354354
assert fakerequest.call_args[0][2] == "jobs"
355355
assert fakerequest.call_args[1]["params"] == {"build": "foo"}
356356
assert fakeclones.call_count == 1
357+
358+
def test_client_errors(self):
359+
"""Test creation of exceptions"""
360+
err = oqe.RequestError("GET", "http://localhost", 404)
361+
assert err.args[0] == "GET"
362+
assert err.args[1] == "http://localhost"
363+
assert err.args[2] == 404
364+
assert err.method == "GET"
365+
assert err.url == "http://localhost"
366+
assert err.status_code == 404
367+
368+
err = oqe.ConnectionError("oh no")
369+
assert err.args[0] == "oh no"
370+
assert err.err == "oh no"

0 commit comments

Comments
 (0)