diff options
author | Frederick Muriuki Muriithi | 2025-07-31 12:50:38 -0500 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2025-07-31 12:59:06 -0500 |
commit | 560dd6accc41cce6626c7b1e9341780f26bc0d4c (patch) | |
tree | 64a6eeaf0f66ec9682b81d9e027245e31e97f761 | |
parent | 24c731e707643395a0505e95e76a15c36baeee3f (diff) | |
download | gn-auth-560dd6accc41cce6626c7b1e9341780f26bc0d4c.tar.gz |
Set HTTP response code from the exception if present.
-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 ( |