Skip to content

Use less generic exceptions #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions gql/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def __init__(
session.fetch_schema()

def validate(self, document):
if not self.schema:
raise Exception(
"Cannot validate the document locally, you need to pass a schema."
)
assert (
self.schema
), "Cannot validate the document locally, you need to pass a schema."

validation_errors = validate(self.schema, document)
if validation_errors:
raise validation_errors[0]
Expand Down
2 changes: 1 addition & 1 deletion gql/dsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def selection_field(field):
if isinstance(field, DSLField):
return field

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


def query(*fields, **kwargs):
Expand Down
6 changes: 2 additions & 4 deletions gql/gql.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from graphql import Source, parse
from graphql import DocumentNode, Source, parse


def gql(request_string):
if not isinstance(request_string, str):
raise Exception(f'Received incompatible request "{request_string}".')
def gql(request_string: str) -> DocumentNode:
source = Source(request_string, "GraphQL request")
return parse(source)
2 changes: 1 addition & 1 deletion scripts/gql-cli
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def main():
elif scheme in ["http", "https"]:
transport = AIOHTTPTransport(url=args.server)
else:
raise Exception("URL protocol should be one of: http, https, ws, wss")
raise ValueError("URL protocol should be one of: http, https, ws, wss")

async with Client(transport=transport) as session:

Expand Down
4 changes: 2 additions & 2 deletions tests/starwars/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def validation_errors(client, query):


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


def test_nested_query_with_fragment(client):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def test_retries_on_transport(execute_mock):


def test_no_schema_exception():
with pytest.raises(Exception) as exc_info:
with pytest.raises(AssertionError) as exc_info:
client = Client()
client.validate("")
assert "Cannot validate the document locally, you need to pass a schema." in str(
Expand Down