Skip to content

context

pyvider.hcl.parser.context

HCL parsing with enhanced error context.

Classes

Functions

parse_with_context

parse_with_context(
    content: str, source_file: Path | None = None
) -> Any

Parse HCL content with enhanced error context.

This function parses HCL content and provides rich error context if parsing fails. It returns the raw parsed data (dict/list), not CTY values.

Parameters:

Name Type Description Default
content str

HCL content string to parse

required
source_file Path | None

Optional source file path for error reporting

None

Returns:

Type Description
Any

Raw parsed data (typically dict or list)

Raises:

Type Description
HclParsingError

If parsing fails, with source location information

Example

content = 'name = "example"' data = parse_with_context(content) data['name'] 'example'

Source code in pyvider/hcl/parser/context.py
def parse_with_context(content: str, source_file: Path | None = None) -> Any:
    """Parse HCL content with enhanced error context.

    This function parses HCL content and provides rich error context if parsing fails.
    It returns the raw parsed data (dict/list), not CTY values.

    Args:
        content: HCL content string to parse
        source_file: Optional source file path for error reporting

    Returns:
        Raw parsed data (typically dict or list)

    Raises:
        HclParsingError: If parsing fails, with source location information

    Example:
        >>> content = 'name = "example"'
        >>> data = parse_with_context(content)
        >>> data['name']
        'example'
    """
    source_str = str(source_file) if source_file else "string input"

    try:
        return hcl2.loads(content)  # type: ignore[attr-defined]
    except Exception as e:
        logger.error(
            "HCL parsing failed",
            source=source_str,
            error=str(e),
            exc_info=True,
        )
        raise HclParsingError(
            message=str(e),
            source_file=str(source_file) if source_file else None,
        ) from e