Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/pyinfra/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
FactError,
FactTypeError,
FactValueError,
FactProcessError,
InventoryError,
OperationError,
OperationTypeError,
Expand Down
6 changes: 6 additions & 0 deletions src/pyinfra/api/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class FactValueError(FactError, ValueError):
"""


class FactProcessError(FactError, RuntimeError):
"""
Exception raised when the data gathered for a fact cannot be processed.
"""


class OperationError(PyinfraError):
"""
Exception raised during fact gathering staging if an operation is unable to
Expand Down
18 changes: 17 additions & 1 deletion src/pyinfra/api/facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from pyinfra import logger
from pyinfra.api import StringCommand
from pyinfra.api.arguments import all_global_arguments, pop_global_arguments
from pyinfra.api.exceptions import FactProcessError
from pyinfra.api.util import (
get_kwargs_str,
log_error_or_warning,
Expand Down Expand Up @@ -269,7 +270,22 @@ def _get_fact(

if status:
if stdout_lines:
data = fact.process(stdout_lines)
try:
data = fact.process(stdout_lines)
except FactProcessError as e:
log_error_or_warning(
host,
global_kwargs["_ignore_errors"],
description=("could not process fact: {0} {1}").format(
name, get_kwargs_str(fact_kwargs)
),
exception=e,
)

# Check we've not failed
if apply_failed_hosts and not global_kwargs["_ignore_errors"]:
state.fail_hosts({host})

elif stderr_lines:
# If we have error output and that error is sudo or su stating the user
# does not exist, do not fail but instead return the default fact value.
Expand Down
16 changes: 15 additions & 1 deletion src/pyinfra/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,11 @@ def log_operation_start(


def log_error_or_warning(
host: "Host", ignore_errors: bool, description: str = "", continue_on_error: bool = False
host: "Host",
ignore_errors: bool,
description: str = "",
continue_on_error: bool = False,
exception: Exception | None = None,
) -> None:
log_func = logger.error
log_color = "red"
Expand All @@ -234,6 +238,16 @@ def log_error_or_warning(
if description:
log_text = f"{log_text}: "

if exception:
exc = exception.__cause__ or exception
exc_text = "{0}: {1}".format(type(exc).__name__, exc)
log_func(
"{0}{1}".format(
host.print_prefix,
click.style(exc_text, log_color),
),
)

log_func(
"{0}{1}{2}".format(
host.print_prefix,
Expand Down