1# exceptions.py - collection of stdnum exceptions
2# coding: utf-8
3#
4# Copyright (C) 2013 Arthur de Jong
5#
6# This library is free software; you can redistribute it and/or
7# modify it under the terms of the GNU Lesser General Public
8# License as published by the Free Software Foundation; either
9# version 2.1 of the License, or (at your option) any later version.
10#
11# This library is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14# Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public
17# License along with this library; if not, write to the Free Software
18# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19# 02110-1301 USA
20
21"""Collection of exceptions.
22
23The validation functions of stdnum should raise one of the below exceptions
24when validation of the number fails.
25"""
26
27
28class ValidationError(Exception):
29    """Top-level error for validating numbers.
30
31    This exception should normally not be raised, only subclasses of this
32    exception."""
33
34    def __str__(self):
35        """Return the exception message."""
36        return ''.join(self.args[:1]) or getattr(self, 'message', '')
37
38
39class InvalidFormat(ValidationError):
40    """Something is wrong with the format of the number.
41
42    This generally means characters or delimiters that are not allowed are
43    part of the number or required parts are missing."""
44
45    message = 'The number has an invalid format.'
46
47
48class InvalidChecksum(ValidationError):
49    """The number's internal checksum or check digit does not match."""
50
51    message = "The number's checksum or check digit is invalid."
52
53
54class InvalidLength(InvalidFormat):
55    """The length of the number is wrong."""
56
57    message = 'The number has an invalid length.'
58
59
60class InvalidComponent(ValidationError):
61    """One of the parts of the number has an invalid reference.
62
63    Some part of the number refers to some external entity like a country
64    code, a date or a predefined collection of values. The number contains
65    some invalid reference."""
66
67    message = 'One of the parts of the number are invalid or unknown.'
68