Skip to content

Commit

Permalink
add better default error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Rodrigues committed Feb 28, 2019
1 parent c2e8e72 commit 9e79de8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ nosetests.xml
coverage.xml
*,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
Expand Down
27 changes: 20 additions & 7 deletions functionapprest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,16 @@ def _options_response(req: Request, methods: list):
return Response(body, 200, headers)


def default_error_handler(error, method):
def default_error_handler(error, method: str):
logging_message = "[%s][{status_code}]: {message}" % method
logging.exception(logging_message.format(
status_code=500,
message=str(error)
))
return ({
'statusCode': 500,
'message': str(error),
}, 500)


def create_functionapp_handler(error_handler=default_error_handler):
Expand Down Expand Up @@ -352,7 +356,10 @@ def inner_functionapp_handler(req: Request, context: FunctionsContext):
method_name = req.method.lower()
func = None
kwargs = {}
error_tuple = ('Internal server error', 500)
error_tuple = ({
'statusCode': 500,
'message': 'Internal server error',
}, 500)
logging_message = "[%s][{status_code}]: {message}" % method_name
try:
# bind the mapping to an empty server name
Expand All @@ -365,12 +372,15 @@ def inner_functionapp_handler(req: Request, context: FunctionsContext):
# if this is a catch-all rule, don't send any kwargs
if rule.rule == '/<path:path>':
kwargs = {}
if req.proxy is not None:
req.route_params = kwargs
# if req.proxy is not None:
# req.route_params = kwargs
except NotFound as e:
logging.warning(logging_message.format(
status_code=404, message=str(e)))
error_tuple = (str(e), 404)
error_tuple = ({
'statusCode': 404,
'message': str(e),
}, 404)

if func:
try:
Expand Down Expand Up @@ -399,11 +409,14 @@ def inner_functionapp_handler(req: Request, context: FunctionsContext):
']['.join(error.absolute_schema_path), error.message)
logging.warning(logging_message.format(
status_code=400, message=error_description))
error_tuple = ('Validation Error', 400)
error_tuple = ({
'statusCode': 404,
'message': f"Validation Error: {error_description}",
}, 400)

except Exception as error:
if error_handler:
error_handler(error, method_name)
error_tuple = error_handler(error, method_name)
else:
raise

Expand Down
2 changes: 1 addition & 1 deletion functionapprest/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__author__ = 'eduardomourar'
__email__ = 'eduardo_demoura@yahoo.com.br'
__version__ = '0.1.1'
__version__ = '0.1.2'
6 changes: 4 additions & 2 deletions tests/test_functionapprest.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ def test_schema_invalid(self):
self.functionapp_handler.handle('post', schema=post_schema)(
post_mock) # decorate mock
result = self.functionapp_handler(self.event, self.context).to_json()
assert result == {'body': '"Validation Error"',
'status_code': 400, 'headers': {}}
assert result == {
'body': '{"statusCode": 404, "message": "Validation Error: Schema[properties][body][properties][my_integer][type] with value \'this is not an integer\' is not of type \'integer\'"}',
'status_code': 400,
'headers': {}}

def test_that_it_returns_bad_request_if_not_given_functionapp_proxy_input(self):
json_body = dict(
Expand Down

0 comments on commit 9e79de8

Please sign in to comment.