From 153cc7bef216d258020df2393b26332bb2512804 Mon Sep 17 00:00:00 2001 From: SD Date: Sun, 28 Jan 2024 15:53:07 +0000 Subject: [PATCH] Improve error messages + add release.md requirements (#3361) * Improve error messages on after and before argument + add release.me requirement to README * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add tests + refer to contribution page for contributers * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Add working test for the new error messages * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * remove commented out code --------- Co-authored-by: Sascha Dobbelaere Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- README.md | 3 ++- RELEASE.md | 4 +++ strawberry/relay/types.py | 13 ++++++++-- tests/relay/test_fields.py | 50 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 RELEASE.md diff --git a/README.md b/README.md index c0ac81d7f6..195e7aa87a 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,8 @@ poetry install --with integrations poetry run pytest ``` -This will install all the dependencies (including dev ones) and run the tests. +For all further detail, check out the [Contributing Page](CONTRIBUTING.md) + ### Pre commit diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..97a74c8ea6 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,4 @@ +Release type: patch + +- Improved error message when supplying in incorrect before or after argument with using relay and pagination. +- Add extra PR requirement in README.md diff --git a/strawberry/relay/types.py b/strawberry/relay/types.py index 200452c0f4..7510dc642c 100644 --- a/strawberry/relay/types.py +++ b/strawberry/relay/types.py @@ -808,11 +808,20 @@ def resolve_connection( if after: after_type, after_parsed = from_base64(after) - assert after_type == PREFIX + if after_type != PREFIX: + # When the base64 hash doesnt exist, the after_type seems to return + # arrayconnEction instead of PREFIX. Let's raise a predictable + # instead of "An unknown error occurred." + raise TypeError("Argument 'after' contains a non-existing value.") + start = int(after_parsed) + 1 if before: before_type, before_parsed = from_base64(before) - assert before_type == PREFIX + if before_type != PREFIX: + # When the base64 hash doesnt exist, the after_type seems to return + # arrayconnEction instead of PREFIX. Let's raise a predictable + # instead of "An unknown error occurred. + raise TypeError("Argument 'before' contains a non-existing value.") end = int(before_parsed) if isinstance(first, int): diff --git a/tests/relay/test_fields.py b/tests/relay/test_fields.py index 76ceca95b3..0c0cd4722d 100644 --- a/tests/relay/test_fields.py +++ b/tests/relay/test_fields.py @@ -1574,3 +1574,53 @@ class Query: } ''' assert str(schema) == textwrap.dedent(expected).strip() + + +before_after_test_query = """ +query fruitsBeforeAfterTest ( + $before: String = null, + $after: String = null, +) { + fruits ( + before: $before + after: $after + ) { + edges { + cursor + node { + id + } + } + } +} +""" + + +async def test_query_before_error(): + """ + Verify if the error raised on a non-existing before hash + raises the correct error + """ + # with pytest.raises(ValueError): + index = to_base64("Fake", 9292292) + result = await schema.execute( + before_after_test_query, + variable_values={"before": index}, + ) + assert result.errors is not None + assert "Argument 'before' contains a non-existing value" in str(result.errors) + + +def test_query_after_error(): + """ + Verify if the error raised on a non-existing before hash + raises the correct error + """ + index = to_base64("Fake", 9292292) + result = schema.execute_sync( + before_after_test_query, + variable_values={"after": index}, + ) + + assert result.errors is not None + assert "Argument 'after' contains a non-existing value" in str(result.errors)