Skip to content

Commit a21ff73

Browse files
authored
Use less generic exceptions (#95)
1 parent 6dde12e commit a21ff73

File tree

6 files changed

+11
-13
lines changed

6 files changed

+11
-13
lines changed

gql/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ def __init__(
7171
session.fetch_schema()
7272

7373
def validate(self, document):
74-
if not self.schema:
75-
raise Exception(
76-
"Cannot validate the document locally, you need to pass a schema."
77-
)
74+
assert (
75+
self.schema
76+
), "Cannot validate the document locally, you need to pass a schema."
77+
7878
validation_errors = validate(self.schema, document)
7979
if validation_errors:
8080
raise validation_errors[0]

gql/dsl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def selection_field(field):
124124
if isinstance(field, DSLField):
125125
return field
126126

127-
raise Exception(f'Received incompatible query field: "{field}".')
127+
raise TypeError(f'Received incompatible query field: "{field}".')
128128

129129

130130
def query(*fields, **kwargs):

gql/gql.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from graphql import Source, parse
1+
from graphql import DocumentNode, Source, parse
22

33

4-
def gql(request_string):
5-
if not isinstance(request_string, str):
6-
raise Exception(f'Received incompatible request "{request_string}".')
4+
def gql(request_string: str) -> DocumentNode:
75
source = Source(request_string, "GraphQL request")
86
return parse(source)

scripts/gql-cli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async def main():
2727
elif scheme in ["http", "https"]:
2828
transport = AIOHTTPTransport(url=args.server)
2929
else:
30-
raise Exception("URL protocol should be one of: http, https, ws, wss")
30+
raise ValueError("URL protocol should be one of: http, https, ws, wss")
3131

3232
async with Client(transport=transport) as session:
3333

tests/starwars/test_validation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ def validation_errors(client, query):
7575

7676

7777
def test_incompatible_request_gql(client):
78-
with pytest.raises(Exception) as exc_info:
78+
with pytest.raises(TypeError) as exc_info:
7979
gql(123)
80-
assert "Received incompatible request" in str(exc_info.value)
80+
assert "body must be a string" in str(exc_info.value)
8181

8282

8383
def test_nested_query_with_fragment(client):

tests/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def test_retries_on_transport(execute_mock):
7070

7171

7272
def test_no_schema_exception():
73-
with pytest.raises(Exception) as exc_info:
73+
with pytest.raises(AssertionError) as exc_info:
7474
client = Client()
7575
client.validate("")
7676
assert "Cannot validate the document locally, you need to pass a schema." in str(

0 commit comments

Comments
 (0)