Last active
October 17, 2018 20:37
-
-
Save sxslex/51729718dd37c67e35994b74cf956955 to your computer and use it in GitHub Desktop.
assert_error_flask.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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