diff --git a/nc_py_api/_exceptions.py b/nc_py_api/_exceptions.py index a366c239..c7524bfe 100644 --- a/nc_py_api/_exceptions.py +++ b/nc_py_api/_exceptions.py @@ -8,12 +8,15 @@ class NextcloudException(Exception): status_code: int reason: str + info: str + response: Response | None - def __init__(self, status_code: int = 0, reason: str = "", info: str = ""): + def __init__(self, status_code: int = 0, reason: str = "", info: str = "", response: Response | None = None): super(BaseException, self).__init__() self.status_code = status_code self.reason = reason self.info = info + self.response = response def __str__(self): reason = f" {self.reason}" if self.reason else "" @@ -24,22 +27,22 @@ def __str__(self): class NextcloudExceptionNotModified(NextcloudException): """The exception indicates that there is no need to retransmit the requested resources.""" - def __init__(self, reason="Not modified", info: str = ""): - super().__init__(304, reason=reason, info=info) + def __init__(self, reason="Not modified", info: str = "", response: Response | None = None): + super().__init__(304, reason=reason, info=info, response=response) class NextcloudExceptionNotFound(NextcloudException): """The exception that is thrown during operations when the object is not found.""" - def __init__(self, reason="Not found", info: str = ""): - super().__init__(404, reason=reason, info=info) + def __init__(self, reason="Not found", info: str = "", response: Response | None = None): + super().__init__(404, reason=reason, info=info, response=response) class NextcloudMissingCapabilities(NextcloudException): """The exception that is thrown when required capability for API is missing.""" - def __init__(self, reason="Missing capability", info: str = ""): - super().__init__(412, reason=reason, info=info) + def __init__(self, reason="Missing capability", info: str = "", response: Response | None = None): + super().__init__(412, reason=reason, info=info, response=response) def check_error(response: Response, info: str = ""): @@ -59,12 +62,12 @@ def check_error(response: Response, info: str = ""): phrase = "Not found" else: phrase = "Unknown error" - raise NextcloudException(status_code, reason=phrase, info=info) + raise NextcloudException(status_code, reason=phrase, info=info, response=response) try: response.raise_for_status() except HTTPError as e: - raise NextcloudException(status_code, reason=response.reason, info=info) from e + raise NextcloudException(status_code, reason=response.reason, info=info, response=response) from e class ModelFetchError(Exception): diff --git a/nc_py_api/_session.py b/nc_py_api/_session.py index 6ecf393d..ba27cc07 100644 --- a/nc_py_api/_session.py +++ b/nc_py_api/_session.py @@ -236,10 +236,12 @@ def ocs( self.init_adapter(restart=True) return self.ocs(method, path, **kwargs, content=content, json=json, params=params, nested_req=True) if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND): - raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info) + raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info, response=response) if ocs_meta["statuscode"] == 304: - raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info) - raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info) + raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info, response=response) + raise NextcloudException( + status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info, response=response + ) return response_data["ocs"]["data"] def update_server_info(self) -> None: @@ -363,10 +365,12 @@ async def ocs( method, path, **kwargs, content=content, json=json, params=params, nested_req=True ) if ocs_meta["statuscode"] in (404, OCSRespond.RESPOND_NOT_FOUND): - raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info) + raise NextcloudExceptionNotFound(reason=ocs_meta["message"], info=info, response=response) if ocs_meta["statuscode"] == 304: - raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info) - raise NextcloudException(status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info) + raise NextcloudExceptionNotModified(reason=ocs_meta["message"], info=info, response=response) + raise NextcloudException( + status_code=ocs_meta["statuscode"], reason=ocs_meta["message"], info=info, response=response + ) return response_data["ocs"]["data"] async def update_server_info(self) -> None: diff --git a/nc_py_api/files/_files.py b/nc_py_api/files/_files.py index 5276201f..e7c3cff5 100644 --- a/nc_py_api/files/_files.py +++ b/nc_py_api/files/_files.py @@ -360,10 +360,14 @@ def lf_parse_webdav_response( def _webdav_response_to_records(webdav_res: Response, info: str) -> list[dict]: check_error(webdav_res, info=info) if webdav_res.status_code != 207: # multistatus - raise NextcloudException(webdav_res.status_code, "Response is not a multistatus.", info=info) + raise NextcloudException( + webdav_res.status_code, "Response is not a multistatus.", info=info, response=webdav_res + ) response_data = loads(dumps(xmltodict.parse(webdav_res.text))) if "d:error" in response_data: err = response_data["d:error"] - raise NextcloudException(reason=f'{err["s:exception"]}: {err["s:message"]}'.replace("\n", ""), info=info) + raise NextcloudException( + reason=f'{err["s:exception"]}: {err["s:message"]}'.replace("\n", ""), info=info, response=webdav_res + ) response = response_data["d:multistatus"].get("d:response", []) return [response] if isinstance(response, dict) else response