policies: improve recording of error messages during policy process

This commit is contained in:
Jens Langhammer 2021-01-16 16:38:57 +01:00
parent f8b4b92e8d
commit 4f28a89e63
2 changed files with 10 additions and 1 deletions

View File

@ -55,6 +55,10 @@ class PolicyEvaluator(BaseEvaluator):
def handle_error(self, exc: Exception, expression_source: str):
"""Exception Handler"""
# So, this is a bit questionable. Essentially, we are edit the stacktrace
# so the user only sees information relevant to them
# and none of our surrounding error handling
exc.__traceback__ = exc.__traceback__.tb_next
raise PolicyException(exc)
def evaluate(self, expression_source: str) -> PolicyResult:

View File

@ -15,6 +15,7 @@ from authentik.policies.models import PolicyBinding
from authentik.policies.types import PolicyRequest, PolicyResult
LOGGER = get_logger()
TRACEBACK_HEADER = "Traceback (most recent call last):\n"
def cache_key(binding: PolicyBinding, request: PolicyRequest) -> str:
@ -85,7 +86,11 @@ class PolicyProcess(Process):
except PolicyException as exc:
# Either use passed original exception or whatever we have
src_exc = exc.src_exc if exc.src_exc else exc
error_string = "".join(format_tb(src_exc.__traceback__)) + str(src_exc)
error_string = (
TRACEBACK_HEADER
+ "".join(format_tb(src_exc.__traceback__))
+ str(src_exc)
)
# Create policy exception event
self.create_event(EventAction.POLICY_EXCEPTION, message=error_string)
LOGGER.debug("P_ENG(proc): error", exc=exc)