Language: Python
Label: safe
CWE: CWE-209 \u2014 Generation of Error Message Containing Sensitive Information
The product generates an error message that includes sensitive information about its environment, users, or associated data.

Code:
def format_error(error, handled_exceptions):
    result: Dict[str, Any]
    if isinstance(error, GraphQLError):
        result = format_graphql_error(error)
    else:
        result = {"message": str(error)}

    if "extensions" not in result:
        result["extensions"] = {}

    exc = error
    while isinstance(exc, GraphQLError) and hasattr(exc, "original_error"):
        exc = exc.original_error
    if isinstance(exc, AssertionError):
        exc = GraphQLError(str(exc))
    if isinstance(exc, handled_exceptions):
        handled_errors_logger.info("A query had an error", exc_info=exc)
    else:
        unhandled_errors_logger.error("A query failed unexpectedly", exc_info=exc)

    # If DEBUG mode is disabled we allow only certain error messages to be returned in
    # the API. This prevents from leaking internals that might be included in Python
    # exceptions' error messages.
    if type(exc) not in ALLOWED_ERRORS and not settings.DEBUG:
        result["message"] = INTERNAL_ERROR_MESSAGE

    result["extensions"]["exception"] = {"code": type(exc).__name__}
    if settings.DEBUG:
        lines = []

        if isinstance(exc, BaseException):
            for line in traceback.format_exception(type(exc), exc, exc.__traceback__):
                lines.extend(line.rstrip().splitlines())
        result["extensions"]["exception"]["stacktrace"] = lines
    return result