From b987c37644db05639ac1542c1311931d60660c4f Mon Sep 17 00:00:00 2001 From: Hanusz Leszek Date: Tue, 26 May 2020 17:28:39 +0200 Subject: [PATCH] Use more generic exceptions --- gql/client.py | 8 ++++---- gql/dsl.py | 2 +- gql/gql.py | 6 ++---- scripts/gql-cli | 2 +- tests/starwars/test_validation.py | 4 ++-- tests/test_client.py | 2 +- 6 files changed, 11 insertions(+), 13 deletions(-) diff --git a/gql/client.py b/gql/client.py index 76cc2395..c961ceb3 100644 --- a/gql/client.py +++ b/gql/client.py @@ -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] diff --git a/gql/dsl.py b/gql/dsl.py index da23887a..6407f267 100644 --- a/gql/dsl.py +++ b/gql/dsl.py @@ -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): diff --git a/gql/gql.py b/gql/gql.py index d8420845..221710ed 100644 --- a/gql/gql.py +++ b/gql/gql.py @@ -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) diff --git a/scripts/gql-cli b/scripts/gql-cli index 533c72ff..c2e8314b 100755 --- a/scripts/gql-cli +++ b/scripts/gql-cli @@ -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: diff --git a/tests/starwars/test_validation.py b/tests/starwars/test_validation.py index a5854aa5..05feb1ea 100644 --- a/tests/starwars/test_validation.py +++ b/tests/starwars/test_validation.py @@ -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): diff --git a/tests/test_client.py b/tests/test_client.py index 4cc38d04..4bc81af5 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -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(