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 (UnsignedDataError)

Compression error.

Means that given data could not be compressed.

Source code in blake2signer/errors.py
class CompressionError(UnsignedDataError):
    """Compression error.

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

ConversionError (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
class ConversionError(SignedDataError, UnsignedDataError):
    """Conversion error.

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

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

Source code in blake2signer/errors.py
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 (SignedDataError)

Decode error.

Means that given data could not be decoded.

Source code in blake2signer/errors.py
class DecodeError(SignedDataError):
    """Decode error.

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

DecompressionError (SignedDataError)

Decompression error.

Means that given data could not be decompressed.

Source code in blake2signer/errors.py
class DecompressionError(SignedDataError):
    """Decompression error.

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

EncodeError (UnsignedDataError)

Encode error.

Means that given data could not be encoded.

Source code in blake2signer/errors.py
class EncodeError(UnsignedDataError):
    """Encode error.

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

ExpiredSignatureError (InvalidSignatureError)

Expired signature error.

Means that the signature has expired.

Source code in blake2signer/errors.py
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) -> 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.
        """
        super().__init__(*args)

        self.timestamp: datetime = timestamp

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

Initialize self.

Parameters:

Name Type Description Default
*args Any

Additional positional arguments, see Exception.__init__.

()

Keyword arguments:

Name Type Description
timestamp datetime

An aware datetime object indicating when the signature was done.

Source code in blake2signer/errors.py
def __init__(self, *args: typing.Any, timestamp: datetime) -> 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.
    """
    super().__init__(*args)

    self.timestamp: datetime = timestamp

FileError (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
class FileError(SignedDataError, UnsignedDataError):
    """File error.

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

InvalidOptionError (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
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 (SignatureError)

Invalid signature error.

Means that the signature is not valid.

Source code in blake2signer/errors.py
class InvalidSignatureError(SignatureError):
    """Invalid signature error.

    Means that the signature is not valid.
    """

MissingDependencyError (SignerError)

Missing dependency error.

Means that a required dependency is not installed.

Source code in blake2signer/errors.py
class MissingDependencyError(SignerError):
    """Missing dependency error.

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

SerializationError (UnsignedDataError)

Serialization error.

Means that given data could not be serialized.

Source code in blake2signer/errors.py
class SerializationError(UnsignedDataError):
    """Serialization error.

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

SignatureError (SignedDataError)

Signature error.

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

Source code in blake2signer/errors.py
class SignatureError(SignedDataError):
    """Signature error.

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

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

Source code in blake2signer/errors.py
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 (Exception)

Base exception for all errors.

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

UnserializationError (SignedDataError)

Unserialization error.

Means that given data could not be unserialized.

Source code in blake2signer/errors.py
class UnserializationError(SignedDataError):
    """Unserialization error.

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

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

Source code in blake2signer/errors.py
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`).
    """
Back to top