Skip to content

sob.errors

DefinitionExistsError

Bases: Exception

This error is raised when an attempt is made to redefine a singleton class instance.

ValidationError

Bases: Exception

This error is raised when sob.validate encounters extraneous attributes associated with a model instance, or discovers missing required attributes.

VersionError

Bases: AttributeError

This error is raised when versioning an object fails due to having data which is incompatible with the target version.

DeserializeError

DeserializeError(data: str | bytes, message: str = '')

Bases: ValueError

This error is raised when data is encountered during deserialization which cannot be parsed.

Attributes:

  • data (str | bytes) –

    The data that could not be parsed.

  • message (str) –

    Additional information about the error.

Parameters:

  • data (str | bytes) –

    The data that could not be parsed.

  • message (str, default: '' ) –

    An optional message to include with the error.

Source code in src/sob/errors.py
51
52
53
54
55
56
57
58
59
def __init__(self, data: str | bytes, message: str = "") -> None:
    """
    Parameters:
        data: The data that could not be parsed.
        message: An optional message to include with the error.
    """
    self.data: str | bytes = data
    self.message: str = message
    super().__init__(*((data,) + ((message,) if message else ())))

UnmarshalError

UnmarshalError(
    message: str | None = None,
    data: sob.abc.MarshallableTypes | None = None,
    types: (
        collections.abc.Iterable[sob.abc.Property | type]
        | sob.abc.Types
        | None
    ) = None,
    item_types: (
        collections.abc.Iterable[sob.abc.Property | type]
        | sob.abc.Types
        | None
    ) = None,
    value_types: (
        collections.abc.Iterable[sob.abc.Property | type]
        | sob.abc.Types
        | None
    ) = None,
)

Bases: Exception

This is an error message raised when data cannot be un-marshalled due to not matching metadata specs.

Attributes:

  • message
  • data (sob.abc.MarshallableTypes | None) –
  • types (sob.abc.MarshallableTypes | None) –
  • item_types (sob.abc.MarshallableTypes | None) –
  • value_types (sob.abc.MarshallableTypes | None) –
Source code in src/sob/errors.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def __init__(
    self,
    message: str | None = None,
    data: abc.MarshallableTypes | None = None,
    types: Iterable[abc.Property | type] | abc.Types | None = None,
    item_types: Iterable[abc.Property | type] | abc.Types | None = None,
    value_types: Iterable[abc.Property | type] | abc.Types | None = None,
) -> None:
    self.data: abc.MarshallableTypes | None = data
    error_message_lines: list[str] = []
    # Identify which parameter is being used for type validation
    types_label: str = (
        "item_types"
        if item_types
        else "value_types"
        if value_types
        else "types"
    )
    types = item_types or value_types or types
    if types is None:
        error_message_lines.append(
            "The data provided is not an instance of an un-marshallable "
            "type:\n"
        )
    else:
        error_message_lines.append(
            "The data provided does not match any of the expected types "
            "and/or property definitions:\n"
        )
    error_message_lines.append(f"- data: {indent(represent(data))}")
    if types is None:
        types = abc.MARSHALLABLE_TYPES
        types_label = "un-marshallable types"
    type_representation: str = indent(
        represent(tuple(types)), number_of_spaces=2
    )
    error_message_lines.append(f"- {types_label}: {type_representation}")
    if message:
        error_message_lines += ["", message]
    super().__init__("\n".join(error_message_lines))

get_exception_text

get_exception_text() -> str

When called within an exception, this function returns a text representation of the error matching what is found in traceback.print_exception, but is returned as a string value rather than printing.

Source code in src/sob/errors.py
143
144
145
146
147
148
149
150
def get_exception_text() -> str:
    """
    When called within an exception, this function returns a text
    representation of the error matching what is found in
    `traceback.print_exception`, but is returned as a string value rather than
    printing.
    """
    return "".join(format_exception(*sys.exc_info()))

append_exception_text

append_exception_text(
    error: Exception, message: str
) -> None

Cause message to be appended to an error's exception text.

Source code in src/sob/errors.py
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def append_exception_text(error: Exception, message: str) -> None:
    """
    Cause `message` to be appended to an error's exception text.
    """
    attribute_name: str
    for attribute_name in ("strerror", "msg", "errmsg"):
        attribute_value: str = getattr(error, attribute_name, "")
        if attribute_value:
            setattr(error, attribute_name, f"{attribute_value}{message}")
    found: bool = False
    index: int
    arg: Any
    reversed_args: list[Any] = list(reversed(error.args)) or [""]
    for index, value in enumerate(reversed_args):
        if isinstance(value, str):
            found = True
            reversed_args[index] = f"{value}{message}"
            break
    if found:
        error.args = tuple(reversed(reversed_args))
    else:
        error.args = (message,)