From 902449d35a67fd2a47c6e51bb70cc13d6e1a61b6 Mon Sep 17 00:00:00 2001 From: Raphael Krupinski <3732079+rafalkrupinski@users.noreply.github.com> Date: Tue, 18 Jun 2024 11:48:36 +0200 Subject: [PATCH] fix: Use handler's __str__() in error messages. Related: # --- litestar/handlers/base.py | 10 ---------- litestar/handlers/http_handlers/base.py | 2 +- litestar/handlers/http_handlers/decorators.py | 2 +- .../handlers/websocket_handlers/route_handler.py | 14 ++++---------- 4 files changed, 6 insertions(+), 22 deletions(-) diff --git a/litestar/handlers/base.py b/litestar/handlers/base.py index 47f8fa4ba4..b6d58b398d 100644 --- a/litestar/handlers/base.py +++ b/litestar/handlers/base.py @@ -254,16 +254,6 @@ def handler_name(self) -> str: """ return get_name(unwrap_partial(self.fn)) - @property - def handler_fullname(self) -> str: - """Get the qualified name of the handler function. - - Returns: - Qualified name of the handler function - """ - handler_fn = unwrap_partial(self.fn) - return handler_fn.__module__ + "." + handler_fn.__qualname__ - @property def dependency_name_set(self) -> set[str]: """Set of all dependency names provided in the handler's ownership layers.""" diff --git a/litestar/handlers/http_handlers/base.py b/litestar/handlers/http_handlers/base.py index ef5b6eb461..6ec2a05109 100644 --- a/litestar/handlers/http_handlers/base.py +++ b/litestar/handlers/http_handlers/base.py @@ -574,7 +574,7 @@ def _validate_handler_function(self) -> None: if return_type.annotation is Empty: raise ImproperlyConfiguredException( - f"A return value of a route handler function {self.handler_fullname} 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'." ) diff --git a/litestar/handlers/http_handlers/decorators.py b/litestar/handlers/http_handlers/decorators.py index ec701d798e..513674cfd7 100644 --- a/litestar/handlers/http_handlers/decorators.py +++ b/litestar/handlers/http_handlers/decorators.py @@ -597,7 +597,7 @@ def _validate_handler_function(self) -> None: or is_class_and_subclass(return_annotation, ASGIFileResponse) ): raise ImproperlyConfiguredException( - f"{self.handler_fullname}: Handlers for 'HEAD' requests must not return a value. Either return 'None' or a response type without a body." + f"{self}: Handlers for 'HEAD' requests must not return a value. Either return 'None' or a response type without a body." ) diff --git a/litestar/handlers/websocket_handlers/route_handler.py b/litestar/handlers/websocket_handlers/route_handler.py index 0e1aade8d8..3b3b8f03bf 100644 --- a/litestar/handlers/websocket_handlers/route_handler.py +++ b/litestar/handlers/websocket_handlers/route_handler.py @@ -85,25 +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( - f"{self.handler_fullname}: WebSocket handlers must return 'None'" - ) + raise ImproperlyConfiguredException(f"{self}: WebSocket handlers must return 'None'") if "socket" not in self.parsed_fn_signature.parameters: - raise ImproperlyConfiguredException( - f"{self.handler_fullname}: WebSocket handlers must define a 'socket' parameter" - ) + 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"{self.handler_fullname}: The {param} kwarg is not supported with websocket handlers" + f"{self}: The {param} kwarg is not supported with websocket handlers" ) if not is_async_callable(self.fn): - raise ImproperlyConfiguredException( - f"{self.handler_fullname}: WebSocket handler functions must be asynchronous" - ) + raise ImproperlyConfiguredException(f"{self}: WebSocket handler functions must be asynchronous") websocket = WebsocketRouteHandler