remap_wit_errors

Functionin decoratorsfastly-compute 0.1.3View source
remap_wit_errors(idiomatic_exceptions: Mapping[Any, type[FastlyError]] | None = None) -> Callable

Raise more idiomatic exceptions from a function that returns a WIT result.

A results error case is always wrapped in a generic Err exception by componentize-py. Convert that to a more descriptive exception that can be selectively caught.

Goals: Be idiomatic. Be reasonably efficient. Be readable as documentation. In that order.

Parameters

idiomatic_exceptions: Mapping[Any, type[FastlyError]] | Noneoptional = None

A map of the types of WIT-level Err.values to more informative exception classes. These classes receive the Err.value as a constructor argument. If the value's type is not found in the map, wrap it in an UnexpectedFastlyError.

Enum members may also be used as mapping keys. Exceptions raised based on these receive no constructor arguments, since the values of enum members are generated and meaningless.

Returns