Skip to content

Commit

Permalink
Merge pull request #121 from starlite-api/1.3.6
Browse files Browse the repository at this point in the history
Update error responses to include pydantic errors under the "extra" key
  • Loading branch information
Goldziher authored May 29, 2022
2 parents 319263b + 24cc7fb commit d448645
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,7 @@

- update Starlette to 0.20.1
- add memoization to openAPI schema

[1.3.6]

- updated validation errors to return more useful json objects
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[tool.poetry]
name = "starlite"
version = "1.3.5"
version = "1.3.6"
description = "Light-weight and flexible ASGI API Framework"
authors = ["Na'aman Hirschfeld <[email protected]>"]
maintainers = ["Na'aman Hirschfeld <[email protected]>", "Ashwin Vinod <[email protected]>"]
maintainers = ["Na'aman Hirschfeld <[email protected]>"]
license = "MIT"
readme = "README.md"
homepage = "https://github.com/starlite-api/starlite"
Expand Down
9 changes: 6 additions & 3 deletions starlite/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from http import HTTPStatus
from typing import Any, Dict, Optional
from typing import Any, Dict, List, Optional, Union

from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.status import (
Expand Down Expand Up @@ -30,10 +30,13 @@ class MissingDependencyException(StarLiteException, ImportError):

class HTTPException(StarLiteException, StarletteHTTPException):
status_code = HTTP_500_INTERNAL_SERVER_ERROR
extra: Optional[Dict[str, Any]] = None
extra: Optional[Union[Dict[str, Any], List[Any]]] = None

def __init__( # pylint: disable=super-init-not-called
self, detail: Optional[str] = None, status_code: Optional[int] = None, extra: Optional[Dict[str, Any]] = None
self,
detail: Optional[str] = None,
status_code: Optional[int] = None,
extra: Optional[Union[Dict[str, Any], List[Any]]] = None,
):
if status_code:
self.status_code = status_code
Expand Down
4 changes: 2 additions & 2 deletions starlite/signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Any, ClassVar, Dict, List, Optional, Type, Union, cast

from pydantic import BaseConfig, BaseModel, ValidationError, create_model
from pydantic.error_wrappers import display_errors
from pydantic.fields import Undefined
from pydantic.typing import AnyCallable
from pydantic_factories import ModelFactory
Expand Down Expand Up @@ -54,7 +53,8 @@ def parse_values_from_connection_kwargs(
return output
except ValidationError as e:
raise ValidationException(
detail=f"Validation failed for {connection.method if isinstance(connection, Request) else 'websocket'} {connection.url}:\n\n{display_errors(e.errors())}"
detail=f"Validation failed for {connection.method if isinstance(connection, Request) else 'websocket'} {connection.url}",
extra=e.errors(),
) from e


Expand Down
14 changes: 14 additions & 0 deletions tests/app/test_error_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
TestClient,
create_test_client,
get,
post,
)
from starlite.exceptions import InternalServerException
from tests import Person


def test_default_handle_http_exception_handling() -> None:
Expand Down Expand Up @@ -45,6 +47,18 @@ def test_default_handle_http_exception_handling() -> None:
}


def test_default_handling_of_pydantic_errors() -> None:
@post("/{param:int}")
def my_route_handler(param: int, data: Person) -> None:
...

with create_test_client(my_route_handler) as client:
response = client.post("/123", json={"first_name": "moishe"})
extra = response.json().get("extra")
assert extra is not None
assert len(extra) == 3


def test_using_custom_http_exception_handler() -> None:
@get("/{param:int}")
def my_route_handler(param: int) -> None:
Expand Down

0 comments on commit d448645

Please sign in to comment.