Skip to content

Commit

Permalink
feat: add handler name to exceptions in handler validation. (#3575)
Browse files Browse the repository at this point in the history
* fix: add handler name to exceptions in handler validation.



---------

Co-authored-by: Raphael Krupinski <3732079+rafalkrupinski@users.noreply.github.com>
Co-authored-by: Janek Nouvertné <provinzkraut@posteo.de>
  • Loading branch information
3 people authored Jun 18, 2024
1 parent a1f5b3b commit 3de1b1d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion litestar/handlers/http_handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ def _validate_handler_function(self) -> None:

if return_type.annotation is Empty:
raise ImproperlyConfiguredException(
"A return value of a route handler function should be type annotated. "
f"A return value of a route handler function {self} should be type annotated. "
"If your function doesn't return a value, annotate it as returning 'None'."
)

Expand Down
4 changes: 3 additions & 1 deletion litestar/handlers/http_handlers/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,9 @@ def _validate_handler_function(self) -> None:
or is_class_and_subclass(return_annotation, File)
or is_class_and_subclass(return_annotation, ASGIFileResponse)
):
raise ImproperlyConfiguredException("A response to a head request should not have a body")
raise ImproperlyConfiguredException(
f"{self}: Handlers for 'HEAD' requests must not return a value. Either return 'None' or a response type without a body."
)


class patch(HTTPRouteHandler):
Expand Down
10 changes: 6 additions & 4 deletions litestar/handlers/websocket_handlers/route_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,19 @@ def _validate_handler_function(self) -> None:
super()._validate_handler_function()

if not self.parsed_fn_signature.return_type.is_subclass_of(NoneType):
raise ImproperlyConfiguredException("Websocket handler functions should return 'None'")
raise ImproperlyConfiguredException(f"{self}: WebSocket handlers must return 'None'")

if "socket" not in self.parsed_fn_signature.parameters:
raise ImproperlyConfiguredException("Websocket handlers must set a 'socket' kwarg")
raise ImproperlyConfiguredException(f"{self}: WebSocket handlers must define a 'socket' parameter")

for param in ("request", "body", "data"):
if param in self.parsed_fn_signature.parameters:
raise ImproperlyConfiguredException(f"The {param} kwarg is not supported with websocket handlers")
raise ImproperlyConfiguredException(
f"{self}: The {param} kwarg is not supported with websocket handlers"
)

if not is_async_callable(self.fn):
raise ImproperlyConfiguredException("Functions decorated with 'websocket' must be async functions")
raise ImproperlyConfiguredException(f"{self}: WebSocket handler functions must be asynchronous")


websocket = WebsocketRouteHandler

0 comments on commit 3de1b1d

Please sign in to comment.