Skip to content

Commit

Permalink
Introduces exceptions.SnakesistQueryError
Browse files Browse the repository at this point in the history
It mirrors what existdb might report back as XQuery parsing errors.
  • Loading branch information
funkyfuture committed Dec 24, 2024
1 parent 730cf3b commit c0ea34f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/api_doc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ Resources
.. autofunction:: snakesist.delb_plugins.existdb_loader

.. autoclass:: snakesist.NodeResource

Exceptions
----------

.. automodule:: snakesist.exceptions
28 changes: 28 additions & 0 deletions snakesist/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import httpx
from _delb.nodes import TagNode


class SnakesistError(Exception):
"""Snakesist base exception class"""

Expand All @@ -22,6 +26,30 @@ class SnakesistNotFound(SnakesistReadError):
pass


class SnakesistQueryError(SnakesistError):
"""Informs about query errors."""

__slots__ = ("messages", "path", "payload")

def __init__(self, payload: str, response: httpx.Response):
exception = TagNode.parse(response.content)
assert exception.local_name == "exception"

self.messages: tuple[str, ...] = tuple(
n.full_text for n in exception.css_select("message")
)
self.path = exception.css_select("path").first.full_text
self.payload = payload

super().__init__()

def __str__(self) -> str:
lines = [f"Exceptions raised when querying on collection `{self.path}`:", ""]
lines.extend(f"- {x}" for x in self.messages)
lines.extend(["", "Payload:", "", self.payload])
return "\n".join(lines)


class SnakesistWriteError(SnakesistError):
"""Raised if a reading operation fails"""

Expand Down
7 changes: 6 additions & 1 deletion snakesist/exist_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from snakesist.exceptions import (
SnakesistConfigError,
SnakesistNotFound,
SnakesistQueryError,
SnakesistReadError,
SnakesistWriteError,
)
Expand Down Expand Up @@ -430,12 +431,16 @@ def query(self, query_expression: str) -> TagNode:
:param query_expression: XQuery expression
:return: The query result as a :class:`delb.TagNode` object.
"""
payload = XQUERY_PAYLOAD.substitute(query=query_expression)
response = self.http_client.post(
self.root_collection_url,
headers={"Content-Type": "application/xml"},
content=XQUERY_PAYLOAD.substitute(query=query_expression),
content=payload,
)

if response.status_code == 400:
raise SnakesistQueryError(payload, response)

try:
response.raise_for_status()
except Exception as e:
Expand Down

0 comments on commit c0ea34f

Please sign in to comment.