diff options
Diffstat (limited to 'gn_auth/errors/common.py')
-rw-r--r-- | gn_auth/errors/common.py | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/gn_auth/errors/common.py b/gn_auth/errors/common.py index 7ef18cf..b950c8f 100644 --- a/gn_auth/errors/common.py +++ b/gn_auth/errors/common.py @@ -14,12 +14,22 @@ def add_trace(exc: Exception, errobj: dict) -> dict: "error-trace": "".join(traceback.format_exception(exc)) } +def __status_code__(exc: Exception): + """Fetch the error code for exceptions that have them.""" + error_code_attributes = ( + "code", "error_code", "errorcode", "status_code", "status_code") + for attr in error_code_attributes: + if hasattr(exc, attr): + return getattr(exc, attr) + + return 500 + def build_handler(description: str): """Generic utility to build error handlers.""" def __handler__(exc: Exception): error = (exc.name if hasattr(exc, "name") else exc.__class__.__name__) - status_code = exc.code if hasattr(exc, "code") else 500 + status_code = __status_code__(exc) content_type = request.content_type if bool(content_type) and content_type.lower() == "application/json": return ( |