in api/base.py [0:0]
def __call__(self, func):
"""
Helper for handling exceptions within the lambda function
and returning back appropriate responses
"""
assert func.__name__ not in self.__class__.registry, f"There are 2 routed functions called {func.__name__}"
@functools.wraps(func)
def wrapper(event, context=None):
try:
if event['body'] is None or isinstance(event['body'], str):
event['body'] = json.loads(event.get('body', None) or '{}')
if not isinstance(event['body'], dict):
raise Exception
except Exception:
return Response(body=f"Malformed JSON: {event['body']}", status_code=400).to_response()
try:
response = func(event)
if not isinstance(response, Response):
response = Response(body=response)
return response.to_response()
except ClientError as e:
return Response(body=str(e), status_code=e.status_code).to_response()
except Exception:
return Response(
body=f"Internal Service Exception: {traceback.format_exc()}",
status_code=500
).to_response()
wrapper.route = self
self.__class__.registry[func.__name__] = wrapper
setattr(self.__class__, func.__name__, wrapper)
return wrapper