diff --git a/src/selva/web/application.py b/src/selva/web/application.py index f0b31c3..44c6394 100644 --- a/src/selva/web/application.py +++ b/src/selva/web/application.py @@ -26,7 +26,7 @@ ) from selva.web.converter.error import ( MissingFromRequestImplError, - MissingFromRequestParamImplError, + MissingParamConverterImplError, MissingRequestParamExtractorImplError, ) from selva.web.converter.from_request import FromRequest @@ -289,7 +289,7 @@ async def _params_from_request( value = await maybe_async(converter.from_str, param) result[name] = value else: - raise MissingFromRequestParamImplError(param_type) + raise MissingParamConverterImplError(param_type) else: if converter := await self._find_param_converter( param_type, FromRequest diff --git a/src/selva/web/converter/error.py b/src/selva/web/converter/error.py index 824b2cb..94f99fe 100644 --- a/src/selva/web/converter/error.py +++ b/src/selva/web/converter/error.py @@ -11,7 +11,7 @@ def __init__(self, param_type): self.param_type = param_type -class MissingFromRequestParamImplError(Exception): +class MissingParamConverterImplError(Exception): def __init__(self, param_type): super().__init__( f"no implementation of '{ParamConverter.__name__}' found for type {param_type}" @@ -25,3 +25,9 @@ def __init__(self, param_type): f"no implementation of '{ParamExtractor.__name__}' found for type {param_type}" ) self.param_type = param_type + + +class PathParamNotFoundError(Exception): + def __init__(self, name: str): + super().__init__(f"path parameter '{name}' not found") + self.name = name diff --git a/src/selva/web/converter/param_extractor_impl.py b/src/selva/web/converter/param_extractor_impl.py index 7650dc1..0028155 100644 --- a/src/selva/web/converter/param_extractor_impl.py +++ b/src/selva/web/converter/param_extractor_impl.py @@ -4,6 +4,7 @@ from asgikit.requests import Request from selva.web.converter.decorator import register_param_extractor +from selva.web.converter.error import PathParamNotFoundError __all__ = ( "FromPath", @@ -37,7 +38,11 @@ def extract( else: name = metadata.name or parameter_name - return request["path_params"][name] + param = request["path_params"].get(name) + if not param: + raise PathParamNotFoundError(name) + + return param class FromQuery(FromRequestParam): diff --git a/tests/web/converter/test_from_request_param.py b/tests/web/converter/test_param_converter.py similarity index 100% rename from tests/web/converter/test_from_request_param.py rename to tests/web/converter/test_param_converter.py