|
| 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** |
0 commit comments