Skip to content

Commit 619c5c4

Browse files
committed
Completed errors documentation
1 parent 2201659 commit 619c5c4

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

arangoasync/collection.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,7 @@ async def get(
587587
Raises:
588588
DocumentRevisionError: If the revision is incorrect.
589589
DocumentGetError: If retrieval fails.
590+
DocumentParseError: If the document is malformed.
590591
591592
References:
592593
- `get-a-document <https://docs.arangodb.com/stable/develop/http-api/documents/#get-a-document>`__
@@ -730,6 +731,7 @@ async def insert(
730731
731732
Raises:
732733
DocumentInsertError: If insertion fails.
734+
DocumentParseError: If the document is malformed.
733735
734736
References:
735737
- `create-a-document <https://docs.arangodb.com/stable/develop/http-api/documents/#create-a-document>`__

docs/errno.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ArangoDB error code constants are provided for convenience.
55

66
**Example**
77

8-
.. testcode::
8+
.. code-block:: python
99
1010
from arangoasync import errno
1111
@@ -14,6 +14,9 @@ ArangoDB error code constants are provided for convenience.
1414
assert errno.DOCUMENT_REV_BAD == 1239
1515
assert errno.DOCUMENT_NOT_FOUND == 1202
1616
17+
You can see the full list of error codes in the `errno.py`_ file.
18+
1719
For more information, refer to the `ArangoDB Manual`_.
1820

1921
.. _ArangoDB Manual: https://www.arangodb.com/docs/stable/appendix-error-codes.html
22+
.. _errno.py: https://github.com/arangodb/python-arango-async/blob/main/arangoasync/errno.py

docs/errors.rst

Lines changed: 95 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,115 @@ All python-arango exceptions inherit :class:`arangoasync.exceptions.ArangoError`
55
which splits into subclasses :class:`arangoasync.exceptions.ArangoServerError` and
66
:class:`arangoasync.exceptions.ArangoClientError`.
77

8+
**Example**
9+
10+
.. code-block:: python
11+
12+
from arangoasync.exceptions import ArangoClientError, ArangoServerError
13+
14+
try:
15+
# Some operation that raises an error
16+
except ArangoClientError:
17+
# An error occurred on the client side
18+
except ArangoServerError:
19+
# An error occurred on the server side
20+
21+
822
Server Errors
923
=============
1024

1125
:class:`arangoasync.exceptions.ArangoServerError` exceptions lightly wrap non-2xx
1226
HTTP responses coming from ArangoDB. Each exception object contains the error
1327
message, error code and HTTP request response details.
1428

29+
**Example:**
30+
31+
.. code-block:: python
32+
33+
from arangoasync import ArangoClient, ArangoServerError, DocumentInsertError
34+
from arangoasync.auth import Auth
35+
36+
# Initialize the client for ArangoDB.
37+
async with ArangoClient(hosts="http://localhost:8529") as client:
38+
auth = Auth(username="root", password="passwd")
39+
40+
# Connect to "test" database as root user.
41+
db = await client.db("test", auth=auth)
42+
43+
# Get the API wrapper for "students" collection.
44+
students = db.collection("students")
45+
46+
try:
47+
await students.insert({"_key": "John"})
48+
await students.insert({"_key": "John"}) # duplicate key error
49+
except DocumentInsertError as err:
50+
assert isinstance(err, ArangoServerError)
51+
assert err.source == "server"
52+
53+
msg = err.message # Exception message usually from ArangoDB
54+
err_msg = err.error_message # Raw error message from ArangoDB
55+
code = err.error_code # Error code from ArangoDB
56+
url = err.url # URL (API endpoint)
57+
method = err.http_method # HTTP method (e.g. "POST")
58+
headers = err.http_headers # Response headers
59+
http_code = err.http_code # Status code (e.g. 200)
60+
61+
# You can inspect the ArangoDB response directly.
62+
response = err.response
63+
method = response.method # HTTP method
64+
headers = response.headers # Response headers
65+
url = response.url # Full request URL
66+
success = response.is_success # Set to True if HTTP code is 2XX
67+
raw_body = response.raw_body # Raw string response body
68+
status_txt = response.status_text # Status text (e.g "OK")
69+
status_code = response.status_code # Status code (e.g. 200)
70+
err_code = response.error_code # Error code from ArangoDB
71+
72+
# You can also inspect the request sent to ArangoDB.
73+
request = err.request
74+
method = request.method # HTTP method
75+
endpoint = request.endpoint # API endpoint starting with "/_api"
76+
headers = request.headers # Request headers
77+
params = request.params # URL parameters
78+
data = request.data # Request payload
79+
1580
Client Errors
1681
=============
1782

1883
:class:`arangoasync.exceptions.ArangoClientError` exceptions originate from
19-
python-arango-async client itself. They do not contain error codes nor HTTP request
84+
driver client itself. They do not contain error codes nor HTTP request
2085
response details.
2186

22-
**Example**
87+
**Example:**
2388

2489
.. code-block:: python
2590
26-
from arangoasync.exceptions import ArangoClientError, ArangoServerError
91+
from arangoasync import ArangoClient, ArangoClientError, DocumentParseError
92+
from arangoasync.auth import Auth
2793
28-
try:
29-
# Some operation that raises an error
30-
except ArangoClientError:
31-
# An error occurred on the client side
32-
except ArangoServerError:
33-
# An error occurred on the server side
94+
# Initialize the client for ArangoDB.
95+
async with ArangoClient(hosts="http://localhost:8529") as client:
96+
auth = Auth(username="root", password="passwd")
97+
98+
# Connect to "test" database as root user.
99+
db = await client.db("test", auth=auth)
100+
101+
# Get the API wrapper for "students" collection.
102+
students = db.collection("students")
103+
104+
try:
105+
await students.get({"_id": "invalid_id"}) # malformed document
106+
except DocumentParseError as err:
107+
assert isinstance(err, ArangoClientError)
108+
assert err.source == "client"
109+
110+
# Only the error message is set.
111+
print(err.message)
112+
113+
Exceptions
114+
==========
115+
116+
Below are all exceptions.
117+
118+
.. automodule:: arangoasync.exceptions
119+
:members:

docs/specs.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ python-arango-async.
3434
.. automodule:: arangoasync.connection
3535
:members:
3636

37-
.. automodule:: arangoasync.exceptions
38-
:members:
39-
4037
.. automodule:: arangoasync.http
4138
:members:
4239

0 commit comments

Comments
 (0)