@@ -5,29 +5,115 @@ All python-arango exceptions inherit :class:`arangoasync.exceptions.ArangoError`
5
5
which splits into subclasses :class: `arangoasync.exceptions.ArangoServerError ` and
6
6
:class: `arangoasync.exceptions.ArangoClientError `.
7
7
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
+
8
22
Server Errors
9
23
=============
10
24
11
25
:class: `arangoasync.exceptions.ArangoServerError ` exceptions lightly wrap non-2xx
12
26
HTTP responses coming from ArangoDB. Each exception object contains the error
13
27
message, error code and HTTP request response details.
14
28
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
+
15
80
Client Errors
16
81
=============
17
82
18
83
: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
20
85
response details.
21
86
22
- **Example **
87
+ **Example: **
23
88
24
89
.. code-block :: python
25
90
26
- from arangoasync.exceptions import ArangoClientError, ArangoServerError
91
+ from arangoasync import ArangoClient, ArangoClientError, DocumentParseError
92
+ from arangoasync.auth import Auth
27
93
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:
0 commit comments