Skip to content

Commit 153cc7b

Browse files
sdobbelaereSascha Dobbelaerepre-commit-ci[bot]
authored
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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent c2273ba commit 153cc7b

File tree

4 files changed

+67
-3
lines changed

4 files changed

+67
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ poetry install --with integrations
122122
poetry run pytest
123123
```
124124

125-
This will install all the dependencies (including dev ones) and run the tests.
125+
For all further detail, check out the [Contributing Page](CONTRIBUTING.md)
126+
126127

127128
### Pre commit
128129

RELEASE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Release type: patch
2+
3+
- Improved error message when supplying in incorrect before or after argument with using relay and pagination.
4+
- Add extra PR requirement in README.md

strawberry/relay/types.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,11 +808,20 @@ def resolve_connection(
808808

809809
if after:
810810
after_type, after_parsed = from_base64(after)
811-
assert after_type == PREFIX
811+
if after_type != PREFIX:
812+
# When the base64 hash doesnt exist, the after_type seems to return
813+
# arrayconnEction instead of PREFIX. Let's raise a predictable
814+
# instead of "An unknown error occurred."
815+
raise TypeError("Argument 'after' contains a non-existing value.")
816+
812817
start = int(after_parsed) + 1
813818
if before:
814819
before_type, before_parsed = from_base64(before)
815-
assert before_type == PREFIX
820+
if before_type != PREFIX:
821+
# When the base64 hash doesnt exist, the after_type seems to return
822+
# arrayconnEction instead of PREFIX. Let's raise a predictable
823+
# instead of "An unknown error occurred.
824+
raise TypeError("Argument 'before' contains a non-existing value.")
816825
end = int(before_parsed)
817826

818827
if isinstance(first, int):

tests/relay/test_fields.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,3 +1574,53 @@ class Query:
15741574
}
15751575
'''
15761576
assert str(schema) == textwrap.dedent(expected).strip()
1577+
1578+
1579+
before_after_test_query = """
1580+
query fruitsBeforeAfterTest (
1581+
$before: String = null,
1582+
$after: String = null,
1583+
) {
1584+
fruits (
1585+
before: $before
1586+
after: $after
1587+
) {
1588+
edges {
1589+
cursor
1590+
node {
1591+
id
1592+
}
1593+
}
1594+
}
1595+
}
1596+
"""
1597+
1598+
1599+
async def test_query_before_error():
1600+
"""
1601+
Verify if the error raised on a non-existing before hash
1602+
raises the correct error
1603+
"""
1604+
# with pytest.raises(ValueError):
1605+
index = to_base64("Fake", 9292292)
1606+
result = await schema.execute(
1607+
before_after_test_query,
1608+
variable_values={"before": index},
1609+
)
1610+
assert result.errors is not None
1611+
assert "Argument 'before' contains a non-existing value" in str(result.errors)
1612+
1613+
1614+
def test_query_after_error():
1615+
"""
1616+
Verify if the error raised on a non-existing before hash
1617+
raises the correct error
1618+
"""
1619+
index = to_base64("Fake", 9292292)
1620+
result = schema.execute_sync(
1621+
before_after_test_query,
1622+
variable_values={"after": index},
1623+
)
1624+
1625+
assert result.errors is not None
1626+
assert "Argument 'after' contains a non-existing value" in str(result.errors)

0 commit comments

Comments
 (0)