Skip to content

Commit 4554e4b

Browse files
authored
Documentation Adding error handling doc (#266)
1 parent 9b85b6c commit 4554e4b

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

docs/advanced/error_handling.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Error Handing
2+
=============
3+
4+
Local errors
5+
------------
6+
7+
If gql detects locally that something does not correspond to the GraphQL specification,
8+
then gql may raise a **GraphQLError** from graphql-core.
9+
10+
This may happen for example:
11+
12+
- if your query is not valid
13+
- if your query does not correspond to your schema
14+
- if the result received from the backend does not correspond to the schema
15+
if :code:`parse_results` is set to True
16+
17+
Transport errors
18+
----------------
19+
20+
If an error happens with the transport, then gql may raise a
21+
:class:`TransportError <gql.transport.exceptions.TransportError>`
22+
23+
Here are the possible Transport Errors:
24+
25+
- :class:`TransportProtocolError <gql.transport.exceptions.TransportProtocolError>`:
26+
Should never happen if the backend is a correctly configured GraphQL server.
27+
It means that the answer received from the server does not correspond
28+
to the transport protocol.
29+
30+
- :class:`TransportServerError <gql.transport.exceptions.TransportServerError>`:
31+
There was an error communicating with the server. If this error is received,
32+
then the connection with the server will be closed. This may happen if the server
33+
returned a 404 http header for example.
34+
The http error code is available in the exception :code:`code` attribute.
35+
36+
- :class:`TransportQueryError <gql.transport.exceptions.TransportQueryError>`:
37+
There was a specific error returned from the server for your query.
38+
The message you receive in this error has been created by the backend, not gql!
39+
In that case, the connection to the server is still available and you are
40+
free to try to send other queries using the same connection.
41+
The message of the exception contains the first error returned by the backend.
42+
All the errors messages are available in the exception :code:`errors` attribute.
43+
44+
- :class:`TransportClosed <gql.transport.exceptions.TransportClosed>`:
45+
This exception is generated when the client is trying to use the transport
46+
while the transport was previously closed.
47+
48+
- :class:`TransportAlreadyConnected <gql.transport.exceptions.TransportAlreadyConnected>`:
49+
Exception generated when the client is trying to connect to the transport
50+
while the transport is already connected.
51+
52+
HTTP
53+
^^^^
54+
55+
For HTTP transports, we should get a json response which contain
56+
:code:`data` or :code:`errors` fields.
57+
If that is not the case, then the returned error depends whether the http return code
58+
is below 400 or not.
59+
60+
- json response:
61+
- with data or errors keys:
62+
- no errors key -> no exception
63+
- errors key -> raise **TransportQueryError**
64+
- no data or errors keys:
65+
- http code < 400:
66+
raise **TransportProtocolError**
67+
- http code >= 400:
68+
raise **TransportServerError**
69+
- not a json response:
70+
- http code < 400:
71+
raise **TransportProtocolError**
72+
- http code >= 400:
73+
raise **TransportServerError**

docs/advanced/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ Advanced
66

77
async_advanced_usage
88
logging
9+
error_handling
910
local_schema
1011
dsl_module

docs/modules/gql.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ Sub-Packages
2020

2121
client
2222
transport
23+
transport_exceptions
2324
dsl
2425
utilities

docs/modules/transport_exceptions.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
gql.transport.exceptions
2+
========================
3+
4+
.. currentmodule:: gql.transport.exceptions
5+
6+
.. automodule:: gql.transport.exceptions
7+
:member-order: bysource

gql/transport/exceptions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33

44
class TransportError(Exception):
5+
"""Base class for all the Transport exceptions"""
6+
57
pass
68

79

@@ -18,7 +20,9 @@ class TransportServerError(TransportError):
1820
This exception will close the transport connection.
1921
"""
2022

21-
def __init__(self, message=None, code=None):
23+
code: Optional[int]
24+
25+
def __init__(self, message: str, code: Optional[int] = None):
2226
super(TransportServerError, self).__init__(message)
2327
self.code = code
2428

@@ -29,6 +33,11 @@ class TransportQueryError(Exception):
2933
This exception should not close the transport connection.
3034
"""
3135

36+
query_id: Optional[int]
37+
errors: Optional[List[Any]]
38+
data: Optional[Any]
39+
extensions: Optional[Any]
40+
3241
def __init__(
3342
self,
3443
msg: str,

0 commit comments

Comments
 (0)