Transform Exceptions¶
Exceptions for the bidirectional data transformation engine: chain configuration, primitive execution, patching, and external callable invocation.
Exception hierarchy¶
TransformError (base)
├── TransformConfigError # Invalid chain or primitive configuration
└── TransformChainError # Chain execution failed
├── PrimitiveError # A single primitive failed
│ ├── DecodeError # base64_decode / bytes_decode
│ ├── DecompressError # zlib_decompress (also bomb protection)
│ ├── ParseError # json_parse / xml_parse
│ ├── PatchError # Patch stage (mapping or callable)
│ ├── SerializeError # json_serialize / xml_serialize
│ ├── CompressError # zlib_compress
│ └── EncodeError # base64_encode / bytes_encode
├── CallableImportError # Callable target cannot be imported
└── CallableError # Callable raised at execution
TransformConfigError also inherits from the standard library
ValueError, so callers can catch it via either base class.
Common failure modes¶
TransformConfigErroris raised when a chain definition is invalid (unknown primitive, wrong type, missing required fields, mutually exclusive options set together, callable module not in the whitelist, server reference unknown). Always raised at config-load time, not at first invocation.DecompressErrorcovers both standard zlib failures and the bomb protection trip (max ratio or absolute size exceeded).ParseErroris raised for malformed JSON or XML input. The XML parser also rejects DOCTYPE declarations to prevent XXE attacks.PatchErrorindicates a problem during the patch stage, typically a mapping miss on a non-string field or a type mismatch.CallableImportErroris raised when the callable target cannot be resolved (module not found, function not in module, or module not in thetransforms.security.allowed_callable_moduleswhitelist).CallableErrorwraps any exception raised by an external callable during execution, preserving the original exception via__cause__.
Usage patterns¶
Handling chain configuration errors¶
from kstlib.transform import load_transform_config
from kstlib.transform.exceptions import TransformConfigError
try:
config = load_transform_config()
except TransformConfigError as e:
logger.error(f"Invalid transforms config: {e}")
# Common causes:
# - Unknown primitive name
# - zlib skip_bytes without explicit backward
# - mapping AND callable both set on the same patch
# - callable module not in allowed_callable_modules whitelist
# - composed_patch references an unknown chain
Handling decompression bombs¶
from kstlib.transform import transform
from kstlib.transform.exceptions import DecompressError
try:
result = transform(suspicious_blob, "decode_chain")
except DecompressError as e:
logger.warning(f"Decompression aborted: {e}")
# Either the absolute decompressed size or the ratio limit was hit.
# Default limits: 200 MB absolute, 100x ratio.
Handling external callable failures¶
from kstlib.transform import transform
from kstlib.transform.exceptions import (
CallableImportError,
CallableError,
TransformChainError,
)
try:
result = transform(blob, "patch_dynamic")
except CallableImportError as e:
logger.error(f"Callable cannot be imported: {e}")
# Check that the module path is correct AND that it is whitelisted
# in transforms.security.allowed_callable_modules.
except CallableError as e:
logger.error(f"Callable raised: {e}")
logger.error(f"Original error: {e.__cause__}")
# The wrapped exception is available via __cause__.
Safe wrapper pattern¶
from kstlib.transform import transform
from kstlib.transform.exceptions import TransformError
def safe_transform(blob: str, chain_name: str, **kwargs) -> str | None:
"""Apply a transform chain with comprehensive error handling."""
try:
return transform(blob, chain_name, **kwargs)
except TransformError as e:
logger.error(f"Transform '{chain_name}' failed: {e}")
return None
API reference¶
Specialized exceptions raised by the kstlib.transform module.
Exception hierarchy:
KstlibError
TransformError (base for all transform errors)
TransformConfigError (invalid configuration, also ValueError)
TransformChainError (chain execution failed)
PrimitiveError (single primitive failed)
DecodeError (base64_decode / bytes_decode)
DecompressError (zlib_decompress)
ParseError (json_parse / xml_parse)
PatchError (patch stage)
SerializeError (json_serialize / xml_serialize)
CompressError (zlib_compress)
EncodeError (base64_encode / bytes_encode)
CallableImportError (callable not found)
CallableError (callable raised)
- exception kstlib.transform.exceptions.CallableError(target, reason, *, chain_name=None)[source]
Bases:
TransformChainErrorCallable raised an exception during execution.
- target
The callable target string.
- exception kstlib.transform.exceptions.CallableImportError(target, *, chain_name=None)[source]
Bases:
TransformChainErrorCallable target could not be imported.
- target
The import target string that failed.
- exception kstlib.transform.exceptions.CompressError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorZlib compression failed.
- exception kstlib.transform.exceptions.DecodeError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorBase64 or bytes decoding failed.
- exception kstlib.transform.exceptions.DecompressError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorZlib decompression failed.
- exception kstlib.transform.exceptions.EncodeError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorBase64 or bytes encoding failed.
- exception kstlib.transform.exceptions.ParseError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorJSON or XML parsing failed.
- exception kstlib.transform.exceptions.PatchError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorPatch stage failed.
- exception kstlib.transform.exceptions.PrimitiveError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
TransformChainErrorA single transform primitive failed.
- primitive_name
Name of the primitive that failed.
- __init__(self, message: 'str', *, primitive_name: 'str | None' = None, chain_name: 'str | None' = None) 'None' -> None[source]
Initialize PrimitiveError.
- exception kstlib.transform.exceptions.SerializeError(message, *, primitive_name=None, chain_name=None)[source]
Bases:
PrimitiveErrorJSON or XML serialization failed.
- exception kstlib.transform.exceptions.TransformChainError(message, *, chain_name=None)[source]
Bases:
TransformErrorTransform chain execution failed.
- chain_name
Name of the chain that failed.
- exception kstlib.transform.exceptions.TransformConfigError[source]
Bases:
TransformError,ValueErrorTransform configuration is invalid.
Raised when the transform chain or primitive configuration contains invalid values, missing required fields, or constraint violations.
- exception kstlib.transform.exceptions.TransformError[source]
Bases:
KstlibErrorBase exception for all transform module errors.