Skip to content

Commit dafd3b4

Browse files
authored
feat: include the Response object in NextcloudException (#394)
Fixes # . Changes proposed in this pull request: * Include the Response object for the downstream apps can get the full error response and anything else in the object. Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent 7ebe4c3 commit dafd3b4

3 files changed

Lines changed: 28 additions & 17 deletions

File tree

nc_py_api/_exceptions.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ class NextcloudException(Exception):
88

99
status_code: int
1010
reason: str
11+
info: str
12+
response: Response | None
1113

12-
def __init__(self, status_code: int = 0, reason: str = "", info: str = ""):
14+
def __init__(self, status_code: int = 0, reason: str = "", info: str = "", response: Response | None = None):
1315
super(BaseException, self).__init__()
1416
self.status_code = status_code
1517
self.reason = reason
1618
self.info = info
19+
self.response = response
1720

1821
def __str__(self):
1922
reason = f" {self.reason}" if self.reason else ""
@@ -24,22 +27,22 @@ def __str__(self):
2427
class NextcloudExceptionNotModified(NextcloudException):
2528
"""The exception indicates that there is no need to retransmit the requested resources."""
2629

27-
def __init__(self, reason="Not modified", info: str = ""):
28-
super().__init__(304, reason=reason, info=info)
30+
def __init__(self, reason="Not modified", info: str = "", response: Response | None = None):
31+
super().__init__(304, reason=reason, info=info, response=response)
2932

3033

3134
class NextcloudExceptionNotFound(NextcloudException):
3235
"""The exception that is thrown during operations when the object is not found."""
3336

34-
def __init__(self, reason="Not found", info: str = ""):
35-
super().__init__(404, reason=reason, info=info)
37+
def __init__(self, reason="Not found", info: str = "", response: Response | None = None):
38+
super().__init__(404, reason=reason, info=info, response=response)
3639

3740

3841
class NextcloudMissingCapabilities(NextcloudException):
3942
"""The exception that is thrown when required capability for API is missing."""
4043

41-
def __init__(self, reason="Missing capability", info: str = ""):
42-
super().__init__(412, reason=reason, info=info)
44+
def __init__(self, reason="Missing capability", info: str = "", response: Response | None = None):
45+
super().__init__(412, reason=reason, info=info, response=response)
4346

4447

4548
def check_error(response: Response, info: str = ""):
@@ -59,12 +62,12 @@ def check_error(response: Response, info: str = ""):
5962
phrase = "Not found"
6063
else:
6164
phrase = "Unknown error"
62-
raise NextcloudException(status_code, reason=phrase, info=info)
65+
raise NextcloudException(status_code, reason=phrase, info=info, response=response)
6366

6467
try:
6568
response.raise_for_status()
6669
except HTTPError as e:
67-
raise NextcloudException(status_code, reason=response.reason, info=info) from e
70+
raise NextcloudException(status_code, reason=response.reason, info=info, response=response) from e
6871

6972

7073
class ModelFetchError(Exception):

nc_py_api/_session.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ def ocs(
236236
self.init_adapter(restart=True)
237237
return self.ocs(method, path, **kwargs, content=content, json=json, params=params, nested_req=True)
238238
if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND):
239-
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info)
239+
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info, response=response)
240240
if ocs_meta["statuscode"] == 304:
241-
raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info)
242-
raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info)
241+
raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info, response=response)
242+
raise NextcloudException(
243+
status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info, response=response
244+
)
243245
return response_data["ocs"]["data"]
244246

245247
def update_server_info(self) -> None:
@@ -363,10 +365,12 @@ async def ocs(
363365
method, path, **kwargs, content=content, json=json, params=params, nested_req=True
364366
)
365367
if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND):
366-
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info)
368+
raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info, response=response)
367369
if ocs_meta["statuscode"] == 304:
368-
raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info)
369-
raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info)
370+
raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info, response=response)
371+
raise NextcloudException(
372+
status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info, response=response
373+
)
370374
return response_data["ocs"]["data"]
371375

372376
async def update_server_info(self) -> None:

nc_py_api/files/_files.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,14 @@ def lf_parse_webdav_response(
360360
def _webdav_response_to_records(webdav_res: Response, info: str) -> list[dict]:
361361
check_error(webdav_res, info=info)
362362
if webdav_res.status_code != 207: # multistatus
363-
raise NextcloudException(webdav_res.status_code, "Response is not a multistatus.", info=info)
363+
raise NextcloudException(
364+
webdav_res.status_code, "Response is not a multistatus.", info=info, response=webdav_res
365+
)
364366
response_data = loads(dumps(xmltodict.parse(webdav_res.text)))
365367
if "d:error" in response_data:
366368
err = response_data["d:error"]
367-
raise NextcloudException(reason=f'{err["s:exception"]}: {err["s:message"]}'.replace("\n", ""), info=info)
369+
raise NextcloudException(
370+
reason=f'{err["s:exception"]}: {err["s:message"]}'.replace("\n", ""), info=info, response=webdav_res
371+
)
368372
response = response_data["d:multistatus"].get("d:response", [])
369373
return [response] if isinstance(response, dict) else response

0 commit comments

Comments
 (0)