Skip to content

Commit 49e66be

Browse files
authored
Merge pull request #80 from enter-at/fix/methods-decorator
fix(lambda_handler): ensure decorator forwards handler self
2 parents cacf4eb + e914f37 commit 49e66be

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

lambda_handlers/handlers/lambda_handler.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ def __init__(self, handler: Optional[Callable] = None):
1919

2020
def __call__(self, handler: Callable): # noqa: D102
2121
@wraps(handler)
22-
def wrapper(event, context):
23-
return self._call_handler(handler, event, context)
22+
def wrapper(*args):
23+
try:
24+
handler_self = args[0] if len(args) > 2 else None
25+
return self.after(self._call_handler(handler_self, handler, *self.before(*args[-2:])))
26+
except Exception as exception:
27+
return self.on_exception(exception)
2428

2529
return wrapper
2630

27-
def _call_handler(self, handler: Callable, event: Event, context: LambdaContext) -> Any:
28-
try:
29-
return self.after(handler(*self.before(event, context)))
30-
except Exception as exception:
31-
return self.on_exception(exception)
31+
def _call_handler(self, handler_self, handler, event, context):
32+
if handler_self:
33+
return handler(handler_self, event, context)
34+
35+
return handler(event, context)
3236

3337
def before(self, event: Event, context: LambdaContext) -> Tuple[Event, LambdaContext]:
3438
"""Event method to be called just before the handler is executed."""

lambda_handlers/handlers/tests/test_lambda_handler.py

+37-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def on_exception(self, exception: Exception) -> Any:
3131

3232

3333
@pytest.fixture
34-
def handler():
34+
def function_handler():
3535
@lambda_handler
3636
def handler(event, context):
3737
if context is None:
@@ -41,20 +41,51 @@ def handler(event, context):
4141
return handler
4242

4343

44-
class TestLambdaHandler:
44+
@pytest.fixture
45+
def method_handler():
46+
class Adapter:
47+
@lambda_handler
48+
def __call__(self, event, context):
49+
if context is None:
50+
raise EventAwareException(message='no such context', event=event)
51+
return event
52+
53+
return Adapter()
54+
55+
56+
class TestLambdaHandlerDecorateFunction:
57+
58+
@pytest.fixture
59+
def event(self):
60+
return {'route': []}
61+
62+
def test_call_order(self, function_handler, event):
63+
result = function_handler(event, {})
64+
65+
assert result == event
66+
assert event['route'] == ['before', 'after']
67+
68+
def test_call_exception(self, function_handler, event):
69+
with pytest.raises(EventAwareException, match='no such context'):
70+
function_handler(event, None)
71+
72+
assert event['route'] == ['before', 'on_exception']
73+
74+
75+
class TestLambdaHandlerDecorateMethod:
4576

4677
@pytest.fixture
4778
def event(self):
4879
return {'route': []}
4980

50-
def test_call_order(self, handler, event):
51-
result = handler(event, {})
81+
def test_call_order(self, method_handler, event):
82+
result = method_handler(event, {})
5283

5384
assert result == event
5485
assert event['route'] == ['before', 'after']
5586

56-
def test_call_exception(self, handler, event):
87+
def test_call_exception(self, method_handler, event):
5788
with pytest.raises(EventAwareException, match='no such context'):
58-
handler(event, None)
89+
method_handler(event, None)
5990

6091
assert event['route'] == ['before', 'on_exception']

0 commit comments

Comments
 (0)