Skip to content

Commit a3e2181

Browse files
author
Awais Hussain
committed
Add new exceptions (GQLSyntaxError and GQLServerError)
GQLServerError maps to errors which are generated on the backend server. I.e. the server responds with a <200 OK> but an "errors" field is included within the response. These are often user facing errors and should be handled as such. GQLSyntaxError maps to cases where either the query or the schema is improperly formatted. This usually indicates a mistake in the code.
1 parent ba7ed56 commit a3e2181

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

gql/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from .transport.local_schema import LocalSchemaTransport
77
from .type_adaptor import TypeAdaptor
8+
from .exceptions import GQLServerError, GQLSyntaxError
89

910
log = logging.getLogger(__name__)
1011

@@ -41,7 +42,7 @@ def __init__(self, schema=None, introspection=None, type_def=None, transport=Non
4142

4243
def validate(self, document):
4344
if not self.schema:
44-
raise Exception("Cannot validate locally the document, you need to pass a schema.")
45+
raise GQLSyntaxError("Cannot validate locally the document, you need to pass a schema.")
4546
validation_errors = validate(self.schema, document)
4647
if validation_errors:
4748
raise validation_errors[0]
@@ -52,7 +53,7 @@ def execute(self, document, *args, **kwargs):
5253

5354
result = self._get_result(document, *args, **kwargs)
5455
if result.errors:
55-
raise Exception(str(result.errors[0]))
56+
raise GQLServerError(result.errors[0])
5657

5758
if self.type_adaptor:
5859
result.data = self.type_adaptor.apply(result.data)

gql/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class GQLSyntaxError(Exception):
2+
"""A problem with the GQL query or schema syntax"""
3+
4+
class GQLServerError(Exception):
5+
"""Errors which should be explicitly handled by the calling code"""

0 commit comments

Comments
 (0)