Skip to content

Instantly share code, notes, and snippets.

@sxslex
Last active October 17, 2018 20:37
Show Gist options
  • Save sxslex/51729718dd37c67e35994b74cf956955 to your computer and use it in GitHub Desktop.
Save sxslex/51729718dd37c67e35994b74cf956955 to your computer and use it in GitHub Desktop.
assert_error_flask.py
import json
import flask
from decorator import decorator
def assert_error():
@decorator
def _fn(f: callable, *args, **kwargs):
try:
return f(*args, **kwargs)
except Exception as exception:
import traceback
status_code = 400
code = 400
try:
if hasattr(exception, 'code'):
code = getattr(exception, 'code')
if hasattr(exception, 'status_code'):
status_code = getattr(exception, 'status_code')
if isinstance(status_code, str) and status_code.isdigit():
status_code = int(status_code)
if not isinstance(status_code, int):
status_code = 400
except Exception:
status_code = 400
code = 400
resp = dict(
code=code,
message=str(exception),
traceback=traceback.format_exc()
)
return resp, status_code
return _fn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment