diff --git a/swh/graphql/resolvers/scalars.py b/swh/graphql/resolvers/scalars.py --- a/swh/graphql/resolvers/scalars.py +++ b/swh/graphql/resolvers/scalars.py @@ -10,6 +10,7 @@ from swh.graphql.errors import InvalidInputError from swh.graphql.utils import utils from swh.model import hashutil +from swh.model.exceptions import ValidationError from swh.model.model import TimestampWithTimezone from swh.model.swhids import CoreSWHID @@ -38,7 +39,11 @@ @swhid_scalar.value_parser def validate_swhid(value): - return CoreSWHID.from_string(value) + try: + swhid = CoreSWHID.from_string(value) + except ValidationError as e: + raise InvalidInputError("Invalid SWHID", e) + return swhid @swhid_scalar.serializer diff --git a/swh/graphql/server.py b/swh/graphql/server.py --- a/swh/graphql/server.py +++ b/swh/graphql/server.py @@ -65,7 +65,7 @@ from starlette.middleware.cors import CORSMiddleware from .app import schema - from .errors.handlers import format_error + from .errors import format_error global graphql_cfg diff --git a/swh/graphql/tests/conftest.py b/swh/graphql/tests/conftest.py --- a/swh/graphql/tests/conftest.py +++ b/swh/graphql/tests/conftest.py @@ -10,6 +10,7 @@ from swh.graphql import server as app_server from swh.graphql.app import schema +from swh.graphql.errors import format_error from swh.search import get_search as get_swh_search from swh.storage import get_storage as get_swh_storage @@ -46,7 +47,11 @@ # GraphQL queries are always sent as POST data = request.get_json() success, result = graphql_sync( - schema, data, context_value=request, debug=app.debug + schema, + data, + context_value=request, + debug=app.debug, + error_formatter=format_error, ) status_code = 200 if success else 400 return jsonify(result), status_code diff --git a/swh/graphql/tests/functional/test_content.py b/swh/graphql/tests/functional/test_content.py --- a/swh/graphql/tests/functional/test_content.py +++ b/swh/graphql/tests/functional/test_content.py @@ -93,7 +93,7 @@ errors = utils.get_error_response(client, query_str) # API will throw an error in case of an invalid SWHID assert len(errors) == 1 - assert "Invalid SWHID: invalid syntax" in errors[0]["message"] + assert "Input error: Invalid SWHID" in errors[0]["message"] def test_get_content_with_invalid_hashes(client): diff --git a/swh/graphql/tests/functional/test_directory.py b/swh/graphql/tests/functional/test_directory.py --- a/swh/graphql/tests/functional/test_directory.py +++ b/swh/graphql/tests/functional/test_directory.py @@ -33,7 +33,7 @@ errors = utils.get_error_response(client, query_str) # API will throw an error in case of an invalid SWHID assert len(errors) == 1 - assert "Invalid SWHID: invalid syntax" in errors[0]["message"] + assert "Input error: Invalid SWHID" in errors[0]["message"] def test_get_revision_directory(client): diff --git a/swh/graphql/tests/functional/test_revision.py b/swh/graphql/tests/functional/test_revision.py --- a/swh/graphql/tests/functional/test_revision.py +++ b/swh/graphql/tests/functional/test_revision.py @@ -83,7 +83,7 @@ errors = utils.get_error_response(client, query_str) # API will throw an error in case of an invalid SWHID assert len(errors) == 1 - assert "Invalid SWHID: invalid syntax" in errors[0]["message"] + assert "Input error: Invalid SWHID" in errors[0]["message"] def test_get_revision_as_target(client): diff --git a/swh/graphql/tests/functional/test_snapshot_node.py b/swh/graphql/tests/functional/test_snapshot_node.py --- a/swh/graphql/tests/functional/test_snapshot_node.py +++ b/swh/graphql/tests/functional/test_snapshot_node.py @@ -54,4 +54,4 @@ """ errors = get_error_response(client, query_str) assert len(errors) == 1 - assert "Invalid SWHID: invalid syntax" in errors[0]["message"] + assert "Input error: Invalid SWHID" in errors[0]["message"] diff --git a/swh/graphql/tests/functional/test_swhid_resolve.py b/swh/graphql/tests/functional/test_swhid_resolve.py --- a/swh/graphql/tests/functional/test_swhid_resolve.py +++ b/swh/graphql/tests/functional/test_swhid_resolve.py @@ -28,7 +28,7 @@ errors = utils.get_error_response(client, query_str) # API will throw an error in case of an invalid SWHID assert len(errors) == 1 - assert "Invalid SWHID: invalid syntax" in errors[0]["message"] + assert "Input error: Invalid SWHID" in errors[0]["message"] @pytest.mark.parametrize(