Skip to content

Errors

All exceptions raised by this lib are subclassed from SignerError. Exceptions in this lib are somewhat verbose so debugging or understanding what failed becomes easier, as it can be appreciated in the tree below.

Not a subclass of SignerError?

If a raised exception is not a subclass of SignerError, then something very unexpected happened: please fill a bug report.
Except for a RuntimeError that will happen in ~2106-02-07, if this library is unmaintained by then.

For example, all errors related to signature validation inherit from SignatureError, which in turn inherits from SignedDataError. This means that you can safely catch the latter when dealing with signed data that needs to be verified without worrying on masking other errors such as those produced on class instantiation.

You can catch exceptions at the level you prefer, or i.e. broad catch them, and then log them according to the exception.

"""Catching errors."""

from datetime import timedelta

from blake2signer import Blake2SerializerSigner
from blake2signer import errors

secret = b'there is no encryption here'
data = [102, 117, 99, 107, 32, 116, 104, 101, 32, 112, 111, 108, 105, 99, 101]
max_age = timedelta(hours=1)

signer = Blake2SerializerSigner(secret, max_age=max_age)
try:
    signed = signer.dumps(data)
except errors.SerializationError:
    print('Data could not be serialized')
except errors.CompressionError:
    print('Data could not be compressed')
except errors.EncodeError:
    print('Data could not be encoded')
except errors.UnsignedDataError:
    # Unreachable if all above exceptions are caught
    print('Data could not be processed somehow')
except errors.DataError:
    # Unreachable if all above exceptions are caught
    print('Unknown error while processing data')
except errors.SignerError:
    # Unreachable if all above exceptions are caught
    print('Unknown error')

try:
    unsigned = signer.loads(signed)
except errors.ConversionError:
    print('Signed data could not be converted to bytes, so data can not be processed')
except errors.ExpiredSignatureError as exc:
    print('Signature is valid but expired on', (exc.timestamp + max_age).isoformat())
except errors.InvalidSignatureError:
    print('The signature is not valid, so data can not be trusted!')
except errors.SignatureError:
    print('The signature is not valid because of its format, so data can not be trusted!')
except errors.DecodeError:
    print('It was not possible to decode data even when signature was valid, what could have happened?')
except errors.DecompressionError:
    print('It was not possible to decompress data even when signature was valid, what could have happened?')
except errors.UnserializationError:
    print('It was not possible to unserialize data even when signature was valid, what could have happened?')
except errors.SignedDataError:
    # Unreachable if all above exceptions are caught
    print('The signature is not valid and/or some part of the unsigning process failed, so data can not be trusted!')
except errors.DataError:
    # Unreachable if all above exceptions are caught
    print('Unknown error while processing data')
except errors.SignerError:
    # Unreachable if all above exceptions are caught
    print('Unknown error')
else:
    # Signature and all is good
    print(unsigned)  # [102, 117, 99, 107, 32, 116, 104, 101, 32, 112, 111, 108, 105, 99, 101]

Impossible errors?

Some exceptions should normally never happen such as DecodeError, DecompressionError and UnserializationError. They exist because they are theoretically possible, and achievable by tricking the signer bypassing in-place safeguards (see the tests for examples).

blake2signer.errors

Errors: contains all errors and exceptions raised by this lib.

Note

Here's the hierarchy tree:

SignerError
    |
    |-- InvalidOptionError: given option value is out of bounds, has the wrong
    |                       format or type.
    |
    |-- MissingDependencyError: a required dependency is not installed.
    |
    |
    |-- DataError: generic data error.
            |
            |-- SignedDataError: error that occurred for *signed data*.
            |       |
            |       |-- SignatureError: error encountered while dealing with
            |       |       |           the signature.
            |       |       |
            |       |       |-- InvalidSignatureError: the signature is not
            |       |               |                  valid.
            |       |               |
            |       |               |-- ExpiredSignatureError: the signature
            |       |                                          has expired.
            |       |
            |       |-- UnserializationError: given data could not be
            |       |                         unserialized.
            |       |
            |       |-- DecompressionError: given data could not be decompressed.
            |       |
            |       |-- DecodeError: given data could not be decoded.
            |       |
            |       |-- ConversionError: given data could not be converted
            |       |                    to bytes.
            |       |
            |       |-- FileError: error while reading the file.
            |
            |-- UnsignedDataError: error that occurred for *data to be signed*.
                    |
                    |-- SerializationError: given data could not be serialized.
                    |
                    |-- CompressionError: given data could not be compressed.
                    |
                    |-- EncodeError: given data could not be encoded.
                    |
                    |-- ConversionError: given data could not be converted
                    |                    to bytes.
                    |
                    |-- FileError: error while writing the file.

CompressionError

Bases: UnsignedDataError

Compression error.

Means that given data could not be compressed.

Source code in blake2signer/errors.py
168
169
170
171
172
class CompressionError(UnsignedDataError):
    """Compression error.

    Means that given data could not be compressed.
    """

ConversionError

Bases: SignedDataError, UnsignedDataError

Conversion error.

Means that given data could not be converted to bytes. This can happen for any process.

Source code in blake2signer/errors.py
189
190
191
192
193
194
class ConversionError(SignedDataError, UnsignedDataError):
    """Conversion error.

    Means that given data could not be converted to bytes. This can happen for
    any process.
    """

DataError

Bases: SignerError

Data error.

Generic data error meaning that given data could not be processed correctly.

All exceptions regarding data handling depends on this one, so you can safely catch it to deal with data errors (both signed and to be signed).

Source code in blake2signer/errors.py
76
77
78
79
80
81
82
83
class DataError(SignerError):
    """Data error.

    Generic data error meaning that given data could not be processed correctly.

    All exceptions regarding data handling depends on this one, so you can safely
    catch it to deal with data errors (both signed and to be signed).
    """

DecodeError

Bases: SignedDataError

Decode error.

Means that given data could not be decoded.

Source code in blake2signer/errors.py
175
176
177
178
179
class DecodeError(SignedDataError):
    """Decode error.

    Means that given data could not be decoded.
    """

DecompressionError

Bases: SignedDataError

Decompression error.

Means that given data could not be decompressed.

Source code in blake2signer/errors.py
161
162
163
164
165
class DecompressionError(SignedDataError):
    """Decompression error.

    Means that given data could not be decompressed.
    """

EncodeError

Bases: UnsignedDataError

Encode error.

Means that given data could not be encoded.

Source code in blake2signer/errors.py
182
183
184
185
186
class EncodeError(UnsignedDataError):
    """Encode error.

    Means that given data could not be encoded.
    """

ExpiredSignatureError

Bases: InvalidSignatureError

Expired signature error.

Means that the signature has expired.

Source code in blake2signer/errors.py
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class ExpiredSignatureError(InvalidSignatureError):
    """Expired signature error.

    Means that the signature has expired.
    """

    # ToDo: D417 is a false positive, see https://github.com/PyCQA/pydocstyle/issues/514
    def __init__(self, *args: typing.Any, timestamp: datetime, data: bytes) -> None:  # noqa: D417
        """Initialize self.

        Args:
            *args: Additional positional arguments, see `Exception.__init__`.

        Keyword Args:
            timestamp: An aware datetime object indicating when the signature was done.
            data: Valid unsigned data as bytes (it may be serialized/compressed/encoded when
                raised from a serializer signer).
        """
        super().__init__(*args)

        self.timestamp: datetime = timestamp
        self.data: bytes = data

__init__(*args, timestamp, data)

Initialize self.

Parameters:

Name Type Description Default
*args typing.Any

Additional positional arguments, see Exception.__init__.

()

Other Parameters:

Name Type Description
timestamp datetime

An aware datetime object indicating when the signature was done.

data bytes

Valid unsigned data as bytes (it may be serialized/compressed/encoded when raised from a serializer signer).

Source code in blake2signer/errors.py
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
def __init__(self, *args: typing.Any, timestamp: datetime, data: bytes) -> None:  # noqa: D417
    """Initialize self.

    Args:
        *args: Additional positional arguments, see `Exception.__init__`.

    Keyword Args:
        timestamp: An aware datetime object indicating when the signature was done.
        data: Valid unsigned data as bytes (it may be serialized/compressed/encoded when
            raised from a serializer signer).
    """
    super().__init__(*args)

    self.timestamp: datetime = timestamp
    self.data: bytes = data

FileError

Bases: SignedDataError, UnsignedDataError

File error.

Means that an operation pertaining a file failed. This can happen during dump or load.

Source code in blake2signer/errors.py
197
198
199
200
201
202
class FileError(SignedDataError, UnsignedDataError):
    """File error.

    Means that an operation pertaining a file failed. This can happen during
    `dump` or `load`.
    """

InvalidOptionError

Bases: SignerError

Invalid option error.

Means that given value is out of bounds or has the wrong format or type for the option.

Source code in blake2signer/errors.py
61
62
63
64
65
66
class InvalidOptionError(SignerError):
    """Invalid option error.

    Means that given value is out of bounds or has the wrong format or type for
    the option.
    """

InvalidSignatureError

Bases: SignatureError

Invalid signature error.

Means that the signature is not valid.

Source code in blake2signer/errors.py
116
117
118
119
120
class InvalidSignatureError(SignatureError):
    """Invalid signature error.

    Means that the signature is not valid.
    """

MissingDependencyError

Bases: SignerError

Missing dependency error.

Means that a required dependency is not installed.

Source code in blake2signer/errors.py
69
70
71
72
73
class MissingDependencyError(SignerError):
    """Missing dependency error.

    Means that a required dependency is not installed.
    """

SerializationError

Bases: UnsignedDataError

Serialization error.

Means that given data could not be serialized.

Source code in blake2signer/errors.py
154
155
156
157
158
class SerializationError(UnsignedDataError):
    """Serialization error.

    Means that given data could not be serialized.
    """

SignatureError

Bases: SignedDataError

Signature error.

Means that an error was encountered while dealing with some part of the signature.

Source code in blake2signer/errors.py
108
109
110
111
112
113
class SignatureError(SignedDataError):
    """Signature error.

    Means that an error was encountered while dealing with some part of the
    signature.
    """

SignedDataError

Bases: DataError

Signed data error.

Generic data error that occurred for signed data that is being processed.

All exceptions regarding signed data handling depends on this one, so you can safely catch it to deal with signed data errors (produced during unsign, unsign_parts, loads, loads_parts, or load).

Source code in blake2signer/errors.py
86
87
88
89
90
91
92
93
94
class SignedDataError(DataError):
    """Signed data error.

    Generic data error that occurred for signed data that is being processed.

    All exceptions regarding signed data handling depends on this one, so you can
    safely catch it to deal with signed data errors (produced during `unsign`,
    `unsign_parts`, `loads`, `loads_parts`, or `load`).
    """

SignerError

Bases: Exception

Base exception for all errors.

Source code in blake2signer/errors.py
57
58
class SignerError(Exception):
    """Base exception for all errors."""

UnserializationError

Bases: SignedDataError

Unserialization error.

Means that given data could not be unserialized.

Source code in blake2signer/errors.py
147
148
149
150
151
class UnserializationError(SignedDataError):
    """Unserialization error.

    Means that given data could not be unserialized.
    """

UnsignedDataError

Bases: DataError

Unsigned data error.

Generic data error that occurred for data to be signed that is being processed.

All exceptions regarding non-signed data handling depends on this one, so you can safely catch it to deal with non-signed data errors (produced during sign, sign_parts, dumps, dumps_parts or dump).

Source code in blake2signer/errors.py
 97
 98
 99
100
101
102
103
104
105
class UnsignedDataError(DataError):
    """Unsigned data error.

    Generic data error that occurred for data to be signed that is being processed.

    All exceptions regarding non-signed data handling depends on this one, so you
    can safely catch it to deal with non-signed data errors (produced during
    `sign`, `sign_parts`, `dumps`, `dumps_parts` or `dump`).
    """