Skip to content

Commit

Permalink
feat: only display path in ValidationExceptions (#3064)
Browse files Browse the repository at this point in the history
  • Loading branch information
floxay authored and provinzkraut committed Mar 1, 2024
1 parent e762e5e commit 7d2335c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
7 changes: 6 additions & 1 deletion litestar/_kwargs/extractors.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from litestar.datastructures import Headers
from litestar.datastructures.upload_file import UploadFile
from litestar.datastructures.url import URL
from litestar.enums import ParamType, RequestEncodingType
from litestar.exceptions import ValidationException
from litestar.params import BodyKwarg
Expand Down Expand Up @@ -106,8 +107,12 @@ def extractor(values: dict[str, Any], connection: ASGIConnection) -> None:
values.update(connection_mapping)
except KeyError as e:
param = alias_to_params[e.args[0]]
path = URL.from_components(
path=connection.url.path,
query=connection.url.query,
)
raise ValidationException(
f"Missing required {param.param_type.value} parameter {param.field_alias!r} for url {connection.url}"
f"Missing required {param.param_type.value} parameter {param.field_alias!r} for path {path}"
) from e

return extractor
Expand Down
7 changes: 6 additions & 1 deletion litestar/_signature/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
_validate_signature_dependencies,
)
from litestar.datastructures.state import ImmutableState
from litestar.datastructures.url import URL
from litestar.dto import AbstractDTO, DTOData
from litestar.enums import ParamType, ScopeType
from litestar.exceptions import InternalServerException, ValidationException
Expand Down Expand Up @@ -119,7 +120,11 @@ def _create_exception(cls, connection: ASGIConnection, messages: list[ErrorMessa
for err_message in messages
if ("key" in err_message and err_message["key"] not in cls._dependency_name_set) or "key" not in err_message
]:
return ValidationException(detail=f"Validation failed for {method} {connection.url}", extra=client_errors)
path = URL.from_components(
path=connection.url.path,
query=connection.url.query,
)
return ValidationException(detail=f"Validation failed for {method} {path}", extra=client_errors)
return InternalServerException()

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_kwargs/test_layered_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def my_handler(self) -> dict:
response = client.get("/router/controller/1", params=query, headers=headers)

assert response.status_code == HTTP_400_BAD_REQUEST
assert response.json()["detail"].startswith(f"Missing required {param_type} parameter '{parameter}' for url")
assert response.json()["detail"].startswith(f"Missing required {param_type} parameter '{parameter}' for path")


def test_layered_parameters_defaults_and_overrides() -> None:
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_signature/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No
response = client.get("/?param=thirteen")

assert response.json() == {
"detail": "Validation failed for GET http://testserver.local/?param=thirteen",
"detail": "Validation failed for GET /?param=thirteen",
"extra": [{"key": "param", "message": "Expected `int`, got `str`", "source": "query"}],
"status_code": 400,
}
Expand All @@ -94,7 +94,7 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No
response = client.get("/?param=thirteen")

assert response.json() == {
"detail": "Validation failed for GET http://testserver.local/?param=thirteen",
"detail": "Validation failed for GET /?param=thirteen",
"extra": [{"key": "param", "message": "Expected `int`, got `str`", "source": "query"}],
"status_code": 400,
}
Expand Down

0 comments on commit 7d2335c

Please sign in to comment.