betty.serde.error module

Provide serialization error handling utilities.

exception betty.serde.error.SerdeError[source]

Bases: UserFacingError, ValueError

A (de)serialization error.

Parameters:

message (betty.locale.Localizable)

__init__(message: betty.locale.Localizable)[source]
Parameters:

message (betty.locale.Localizable)

property contexts: tuple[Localizable, ...]

Get the human-readable contexts describing where the error occurred in the source data.

localize(localizer: betty.locale.Localizer) str[source]

Localize self to a human-readable string.

Parameters:

localizer (betty.locale.Localizer)

Return type:

str

raised(error_type: type[betty.serde.error.SerdeError]) bool[source]

Check if the error matches the given error type.

Parameters:

error_type (type[betty.serde.error.SerdeError])

Return type:

bool

with_context(*contexts: betty.locale.Localizable) Self[source]

Add a message describing the error’s context.

Parameters:

contexts (betty.locale.Localizable)

Return type:

typing.Self

exception betty.serde.error.SerdeErrorCollection[source]

Bases: SerdeError

A collection of zero or more (de)serialization errors.

Parameters:

errors (list[betty.serde.error.SerdeError] | None)

__init__(errors: list[betty.serde.error.SerdeError] | None = None)[source]
Parameters:

errors (list[betty.serde.error.SerdeError] | None)

append(*errors: betty.serde.error.SerdeError) None[source]

Append errors to this collection.

Parameters:

errors (betty.serde.error.SerdeError)

Return type:

None

assert_valid() Iterator[Self][source]

Assert that this collection contains no errors.

Return type:

typing.Iterator[typing.Self]

catch(*contexts: betty.locale.Localizable) Iterator[betty.serde.error.SerdeErrorCollection][source]

Catch any errors raised within this context manager and add them to the collection.

Return type:

typing.Iterator[betty.serde.error.SerdeErrorCollection]

Returns:

A new collection that will only contain any newly raised errors.

Parameters:

contexts (betty.locale.Localizable)

property invalid: bool

Check that this collection contains at least one error.

localize(localizer: betty.locale.Localizer) str[source]

Localize self to a human-readable string.

Parameters:

localizer (betty.locale.Localizer)

Return type:

str

raised(error_type: type[betty.serde.error.SerdeError]) bool[source]

Check if the error matches the given error type.

Parameters:

error_type (type[betty.serde.error.SerdeError])

Return type:

bool

property valid: bool

Check that this collection contains no errors.

with_context(*contexts: betty.locale.Localizable) Self[source]

Add a message describing the error’s context.

Parameters:

contexts (betty.locale.Localizable)

Return type:

typing.Self