Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions nc_py_api/_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand All @@ -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 = ""):
Expand All @@ -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):
Expand Down
16 changes: 10 additions & 6 deletions nc_py_api/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions nc_py_api/files/_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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