Skip to content

Commit

Permalink
improvement(error_handlers): Add context dump to the exceptions
Browse files Browse the repository at this point in the history
This commit adds additional information and trace ids to the exception
that occur inside the API calls, allowing to collect more information
about the error, including the request data.

Fixes scylladb#492
  • Loading branch information
k0machi committed Dec 9, 2024
1 parent 420b826 commit 8cde76e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions argus/backend/error_handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import base64
from hashlib import sha256
import logging
import os
from traceback import format_exception
from flask import request

Expand All @@ -12,14 +15,24 @@ class DataValidationError(APIException):
pass

def handle_api_exception(exception: Exception):
trace_id = base64.encodebytes(sha256(os.urandom(64)).digest()).decode(encoding="utf-8").strip()
if issubclass(exception.__class__, APIException):
LOGGER.info("TraceId: %s", trace_id)
LOGGER.info("Endpoint %s responded with error %s: %s", request.endpoint, exception.__class__.__name__, str(exception))
LOGGER.info("Headers\n%s", request.headers)
LOGGER.info("Request Data Start\n%s\nRequest Data End", request.json if request.is_json else request.get_data(as_text=True))
LOGGER.info("TraceId: %s", trace_id)
else:
LOGGER.error("TraceId: %s", trace_id)
LOGGER.error("Exception in %s\n%s", request.endpoint, "".join(format_exception(exception)))
LOGGER.error("Headers\n%s", request.headers)
LOGGER.error("Request Data Start\n%s\nRequest Data End", request.json if request.is_json else request.get_data(as_text=True))
LOGGER.error("TraceId: %s", trace_id)

return {
"status": "error",
"response": {
"trace_id": trace_id,
"exception": exception.__class__.__name__,
"arguments": exception.args
}
Expand Down

0 comments on commit 8cde76e

Please sign in to comment.