diff options
Diffstat (limited to 'cloudinit/sources/azure/errors.py')
-rw-r--r-- | cloudinit/sources/azure/errors.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/cloudinit/sources/azure/errors.py b/cloudinit/sources/azure/errors.py index 1a452635..5c4ad7db 100644 --- a/cloudinit/sources/azure/errors.py +++ b/cloudinit/sources/azure/errors.py @@ -8,7 +8,7 @@ import logging import traceback from datetime import datetime from io import StringIO -from typing import Any, Dict, Optional +from typing import Any, Dict, List, Optional from cloudinit import version from cloudinit.sources.azure import identity @@ -16,6 +16,22 @@ from cloudinit.sources.azure import identity LOG = logging.getLogger(__name__) +def encode_report( + data: List[str], delimiter: str = "|", quotechar: str = "'" +) -> str: + """Encode report data with csv.""" + with StringIO() as io: + csv.writer( + io, + delimiter=delimiter, + quotechar=quotechar, + quoting=csv.QUOTE_MINIMAL, + ).writerow(data) + + # strip trailing \r\n + return io.getvalue().rstrip() + + class ReportableError(Exception): def __init__( self, @@ -39,10 +55,11 @@ class ReportableError(Exception): except Exception as id_error: self.vm_id = f"failed to read vm id: {id_error!r}" - def as_description( - self, *, delimiter: str = "|", quotechar: str = "'" + def as_encoded_report( + self, ) -> str: data = [ + "result=error", f"reason={self.reason}", f"agent={self.agent}", ] @@ -53,18 +70,7 @@ class ReportableError(Exception): f"documentation_url={self.documentation_url}", ] - with StringIO() as io: - csv.writer( - io, - delimiter=delimiter, - quotechar=quotechar, - quoting=csv.QUOTE_MINIMAL, - ).writerow(data) - - # strip trailing \r\n - csv_data = io.getvalue().rstrip() - - return f"PROVISIONING_ERROR: {csv_data}" + return encode_report(data) def __eq__(self, other) -> bool: return ( @@ -75,7 +81,7 @@ class ReportableError(Exception): ) def __repr__(self) -> str: - return self.as_description() + return self.as_encoded_report() class ReportableErrorUnhandledException(ReportableError): |