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.
    |
    |-- 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

Compression error.

Means that given data could not be compressed.

ConversionError

Conversion error.

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

DataError

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

Decode error.

Means that given data could not be decoded.

DecompressionError

Decompression error.

Means that given data could not be decompressed.

EncodeError

Encode error.

Means that given data could not be encoded.

ExpiredSignatureError

Expired signature error.

Means that the signature has expired.

__init__(self, *args, *, timestamp) special

Initialize self.

Parameters:

Name Type Description Default
*args Any

Additional positional arguments, see Exception.__init__.

()
timestamp datetime

An aware datetime object indicating when the signature was done.

required

Returns:

Type Description
None

None.

Source code in blake2signer/errors.py
def __init__(self, *args: typing.Any, timestamp: datetime) -> None:
    """Initialize self.

    Args:
        *args: Additional positional arguments, see `Exception.__init__`.
        timestamp: An aware datetime object indicating when the signature was done.

    Returns:
        None.
    """
    super().__init__(*args)

    self.timestamp: datetime = timestamp

FileError

File error.

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

InvalidOptionError

Invalid option error.

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

InvalidSignatureError

Invalid signature error.

Means that the signature is not valid.

SerializationError

Serialization error.

Means that given data could not be serialized.

SignatureError

Signature error.

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

SignedDataError

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

Base exception for all errors.

UnserializationError

Unserialization error.

Means that given data could not be unserialized.

UnsignedDataError

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).