blob: 791239fa6a54cc0a826d1c53df8c42cba5a9619c [file] [log] [blame]
Guido van Rossum8b3febe2007-08-30 01:15:14 +00001# Copyright (C) 2001-2006 Python Software Foundation
2# Author: Barry Warsaw
3# Contact: email-sig@python.org
4
5"""email package exception classes."""
6
7
Guido van Rossum8b3febe2007-08-30 01:15:14 +00008class MessageError(Exception):
9 """Base class for errors in the email package."""
10
11
12class MessageParseError(MessageError):
13 """Base class for message parsing errors."""
14
15
16class HeaderParseError(MessageParseError):
17 """Error while parsing headers."""
18
19
20class BoundaryError(MessageParseError):
21 """Couldn't find terminating boundary."""
22
23
24class MultipartConversionError(MessageError, TypeError):
25 """Conversion to a multipart is prohibited."""
26
27
28class CharsetError(MessageError):
29 """An illegal charset was given."""
30
31
Guido van Rossum8b3febe2007-08-30 01:15:14 +000032# These are parsing defects which the parser was able to work around.
R David Murray0b6f6c82012-05-25 18:42:14 -040033class MessageDefect(ValueError):
Guido van Rossum8b3febe2007-08-30 01:15:14 +000034 """Base class for a message defect."""
35
36 def __init__(self, line=None):
R David Murray6e50b692012-06-08 22:45:46 -040037 if line is not None:
38 super().__init__(line)
Guido van Rossum8b3febe2007-08-30 01:15:14 +000039 self.line = line
40
41class NoBoundaryInMultipartDefect(MessageDefect):
42 """A message claimed to be a multipart but had no boundary parameter."""
43
44class StartBoundaryNotFoundDefect(MessageDefect):
45 """The claimed start boundary was never found."""
46
R David Murray7ef3ff32012-05-27 22:20:42 -040047class CloseBoundaryNotFoundDefect(MessageDefect):
48 """A start boundary was found, but not the corresponding close boundary."""
49
Guido van Rossum8b3febe2007-08-30 01:15:14 +000050class FirstHeaderLineIsContinuationDefect(MessageDefect):
51 """A message had a continuation line as its first header line."""
52
53class MisplacedEnvelopeHeaderDefect(MessageDefect):
54 """A 'Unix-from' header was found in the middle of a header block."""
55
R David Murrayadbdcdb2012-05-27 20:45:01 -040056class MissingHeaderBodySeparatorDefect(MessageDefect):
57 """Found line with no leading whitespace and no colon before blank line."""
58# XXX: backward compatibility, just in case (it was never emitted).
59MalformedHeaderDefect = MissingHeaderBodySeparatorDefect
Guido van Rossum8b3febe2007-08-30 01:15:14 +000060
61class MultipartInvariantViolationDefect(MessageDefect):
62 """A message claimed to be a multipart but no subparts were found."""
R David Murray749073a2011-06-22 13:47:53 -040063
64class InvalidMultipartContentTransferEncodingDefect(MessageDefect):
65 """An invalid content transfer encoding was set on the multipart itself."""
R David Murray0b6f6c82012-05-25 18:42:14 -040066
67class UndecodableBytesDefect(MessageDefect):
68 """Header contained bytes that could not be decoded"""
69
70class InvalidBase64PaddingDefect(MessageDefect):
71 """base64 encoded sequence had an incorrect length"""
72
73class InvalidBase64CharactersDefect(MessageDefect):
74 """base64 encoded sequence had characters not in base64 alphabet"""
75
76# These errors are specific to header parsing.
77
78class HeaderDefect(MessageDefect):
79 """Base class for a header defect."""
80
R David Murray6e50b692012-06-08 22:45:46 -040081 def __init__(self, *args, **kw):
82 super().__init__(*args, **kw)
83
R David Murray0b6f6c82012-05-25 18:42:14 -040084class InvalidHeaderDefect(HeaderDefect):
85 """Header is not valid, message gives details."""
86
87class HeaderMissingRequiredValue(HeaderDefect):
88 """A header that must have a value had none"""
89
90class NonPrintableDefect(HeaderDefect):
91 """ASCII characters outside the ascii-printable range found"""
92
93 def __init__(self, non_printables):
94 super().__init__(non_printables)
95 self.non_printables = non_printables
96
97 def __str__(self):
98 return ("the following ASCII non-printables found in header: "
99 "{}".format(self.non_printables))
100
101class ObsoleteHeaderDefect(HeaderDefect):
102 """Header uses syntax declared obsolete by RFC 5322"""
103
104class NonASCIILocalPartDefect(HeaderDefect):
105 """local_part contains non-ASCII characters"""
106 # This defect only occurs during unicode parsing, not when
107 # parsing messages decoded from binary.