Skip to content

Commit badc350

Browse files
committed
Amending ellar exception module refactor
1 parent bdf9da6 commit badc350

File tree

23 files changed

+51
-40
lines changed

23 files changed

+51
-40
lines changed

ellar/common/decorators/controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
)
1414
from ellar.core import ControllerBase
1515
from ellar.core.controller import ControllerType
16+
from ellar.core.exceptions import ImproperConfiguration
1617
from ellar.di import RequestScope, injectable
17-
from ellar.exceptions import ImproperConfiguration
1818
from ellar.reflect import reflect
1919

2020
if t.TYPE_CHECKING: # pragma: no cover

ellar/common/decorators/exception.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
class ValidateExceptionHandler(BaseModel):
99
key: t.Union[int, t.Type[Exception]]
10-
value: t.Callable
10+
value: t.Union[t.Callable, t.Type]
1111

1212

13-
def add_exception_handler(
13+
def _add_exception_handler(
1414
exc_class_or_status_code: t.Union[int, t.Type[Exception]],
15-
handler: t.Callable,
15+
handler: t.Union[t.Callable, t.Type],
1616
) -> None:
1717
validator = ValidateExceptionHandler(key=exc_class_or_status_code, value=handler)
1818
exception_handlers = {validator.key: validator.value}
@@ -30,8 +30,8 @@ def exception_handler(
3030
:return: Function
3131
"""
3232

33-
def decorator(func: t.Callable) -> t.Callable:
34-
add_exception_handler(exc_class_or_status_code, func)
33+
def decorator(func: t.Union[t.Callable, t.Type]) -> t.Callable:
34+
_add_exception_handler(exc_class_or_status_code, func)
3535
return func
3636

3737
return decorator

ellar/common/decorators/html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
TEMPLATE_FILTER_KEY,
1010
TEMPLATE_GLOBAL_KEY,
1111
)
12+
from ellar.core.exceptions import ImproperConfiguration
1213
from ellar.core.response.model import HTMLResponseModel
1314
from ellar.core.routing import RouteOperationBase
1415
from ellar.core.templating import TemplateFunctionData
15-
from ellar.exceptions import ImproperConfiguration
1616
from ellar.helper import class_base_function_regex, get_name
1717
from ellar.types import TemplateFilterCallable, TemplateGlobalCallable
1818

ellar/common/decorators/modules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
from ellar.compatible import AttributeDict
99
from ellar.constants import MODULE_METADATA, MODULE_WATERMARK
1010
from ellar.core import ControllerBase
11+
from ellar.core.exceptions import ImproperConfiguration
1112
from ellar.core.modules.base import ModuleBase, ModuleBaseMeta
1213
from ellar.core.routing import ModuleMount, ModuleRouter
1314
from ellar.di import ProviderConfig, SingletonScope, injectable
14-
from ellar.exceptions import ImproperConfiguration
1515
from ellar.reflect import reflect
1616

1717
if t.TYPE_CHECKING: # pragma: no cover

ellar/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def __new__(mcls, name, bases, namespace):
5151
OPENAPI_KEY = "OPENAPI"
5252
VERSIONING_KEY = "ROUTE_VERSIONING"
5353
GUARDS_KEY = "ROUTE_GUARDS"
54-
EXTRA_ROUTE_ARGS_KEY = "EXTRA_ROUTE_ARGSextra_route_args"
54+
EXTRA_ROUTE_ARGS_KEY = "EXTRA_ROUTE_ARGS"
5555
RESPONSE_OVERRIDE_KEY = "RESPONSE_OVERRIDE"
5656
EXCEPTION_HANDLERS_KEY = "EXCEPTION_HANDLERS"
5757

ellar/core/guard/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ellar.core.connection import HTTPConnection
99
from ellar.core.context import ExecutionContext
10-
from ellar.exceptions import APIException
10+
from ellar.core.exceptions import APIException
1111

1212

1313
class GuardCanActivate(ABC, metaclass=ABCMeta):

ellar/core/guard/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from base64 import b64decode
55

66
from ellar.core.connection import HTTPConnection
7-
from ellar.exceptions import APIException, AuthenticationFailed
7+
from ellar.core.exceptions import APIException, AuthenticationFailed
88

99
from .base import BaseHttpAuth, HTTPAuthorizationCredentials, HTTPBasicCredentials
1010

ellar/core/middleware/__init__.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import sys
2-
31
from starlette.middleware import Middleware as Middleware
42
from starlette.middleware.authentication import AuthenticationMiddleware
53
from starlette.middleware.base import BaseHTTPMiddleware
@@ -15,14 +13,9 @@
1513
from starlette.middleware.wsgi import WSGIMiddleware as WSGIMiddleware
1614

1715
from .di import RequestServiceProviderMiddleware
16+
from .exceptions import ExceptionMiddleware
1817
from .versioning import RequestVersioningMiddleware
1918

20-
if sys.version_info >= (3, 7): # pragma: no cover
21-
from starlette.middleware.exceptions import ExceptionMiddleware
22-
else:
23-
from starlette.exceptions import ExceptionMiddleware
24-
25-
2619
__all__ = [
2720
"Middleware",
2821
"AuthenticationMiddleware",

ellar/core/middleware/di.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from ellar.types import ASGIApp, TReceive, TScope, TSend
1717

1818
if t.TYPE_CHECKING: # pragma: no cover
19+
from ellar.core.exceptions.interfaces import IExceptionHandler
1920
from ellar.di.injector import EllarInjector, RequestServiceProvider
2021

2122

@@ -26,10 +27,14 @@ def __init__(
2627
*,
2728
debug: bool,
2829
injector: "EllarInjector",
29-
handler: t.Callable = None
30+
handler: "IExceptionHandler" = None
3031
) -> None:
32+
_handler = None
33+
if handler:
34+
self._500_error_handler = handler
35+
_handler = self.error_handler
3136
super(RequestServiceProviderMiddleware, self).__init__(
32-
debug=debug, handler=handler, app=app
37+
debug=debug, handler=_handler, app=app
3338
)
3439
self.injector = injector
3540

@@ -96,3 +101,9 @@ async def __call__(self, scope: TScope, receive: TReceive, send: TSend) -> None:
96101
scope[SCOPE_SERVICE_PROVIDER] = request_provider
97102
scope[SCOPE_EXECUTION_CONTEXT_PROVIDER] = execute_context
98103
await super().__call__(scope, receive, send)
104+
105+
async def error_handler(self, request: Request, exc: Exception) -> Response:
106+
execute_context = request.scope[SCOPE_EXECUTION_CONTEXT_PROVIDER]
107+
assert self._500_error_handler
108+
response = await self._500_error_handler.catch(ctx=execute_context, exc=exc)
109+
return response

ellar/core/modules/builder.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from ellar.core.events import EventHandler
1313
from ellar.reflect import reflect
1414

15+
from ..exceptions.callable_exceptions import CallableExceptionHandler
1516
from .helper import class_parameter_executor_wrapper
1617

1718
if t.TYPE_CHECKING: # pragma: no cover
@@ -42,8 +43,12 @@ def __init__(self, cls: t.Union[t.Type["ModuleBase"], "ModuleBaseMeta"]) -> None
4243

4344
def exception_config(self, exception_dict: t.Dict) -> None:
4445
for k, v in exception_dict.items():
45-
func = class_parameter_executor_wrapper(self._cls, v)
46-
reflect.define_metadata(EXCEPTION_HANDLERS_KEY, {k: func}, self._cls)
46+
func = CallableExceptionHandler(
47+
self._cls, callable_exception_handler=v, exc_class_or_status_code=k
48+
)
49+
reflect.define_metadata(
50+
EXCEPTION_HANDLERS_KEY, func, self._cls, default_value=[]
51+
)
4752

4853
@t.no_type_check
4954
def middleware_config(self, middleware: "MiddlewareSchema") -> None:

0 commit comments

Comments
 (0)