Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 1 | # 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 Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 8 | class MessageError(Exception): |
| 9 | """Base class for errors in the email package.""" |
| 10 | |
| 11 | |
| 12 | class MessageParseError(MessageError): |
| 13 | """Base class for message parsing errors.""" |
| 14 | |
| 15 | |
| 16 | class HeaderParseError(MessageParseError): |
| 17 | """Error while parsing headers.""" |
| 18 | |
| 19 | |
| 20 | class BoundaryError(MessageParseError): |
| 21 | """Couldn't find terminating boundary.""" |
| 22 | |
| 23 | |
| 24 | class MultipartConversionError(MessageError, TypeError): |
| 25 | """Conversion to a multipart is prohibited.""" |
| 26 | |
| 27 | |
| 28 | class CharsetError(MessageError): |
| 29 | """An illegal charset was given.""" |
| 30 | |
| 31 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 32 | # These are parsing defects which the parser was able to work around. |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 33 | class MessageDefect(ValueError): |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 34 | """Base class for a message defect.""" |
| 35 | |
| 36 | def __init__(self, line=None): |
| 37 | self.line = line |
| 38 | |
| 39 | class NoBoundaryInMultipartDefect(MessageDefect): |
| 40 | """A message claimed to be a multipart but had no boundary parameter.""" |
| 41 | |
| 42 | class StartBoundaryNotFoundDefect(MessageDefect): |
| 43 | """The claimed start boundary was never found.""" |
| 44 | |
R David Murray | 7ef3ff3 | 2012-05-27 22:20:42 -0400 | [diff] [blame] | 45 | class CloseBoundaryNotFoundDefect(MessageDefect): |
| 46 | """A start boundary was found, but not the corresponding close boundary.""" |
| 47 | |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 48 | class FirstHeaderLineIsContinuationDefect(MessageDefect): |
| 49 | """A message had a continuation line as its first header line.""" |
| 50 | |
| 51 | class MisplacedEnvelopeHeaderDefect(MessageDefect): |
| 52 | """A 'Unix-from' header was found in the middle of a header block.""" |
| 53 | |
R David Murray | adbdcdb | 2012-05-27 20:45:01 -0400 | [diff] [blame] | 54 | class MissingHeaderBodySeparatorDefect(MessageDefect): |
| 55 | """Found line with no leading whitespace and no colon before blank line.""" |
| 56 | # XXX: backward compatibility, just in case (it was never emitted). |
| 57 | MalformedHeaderDefect = MissingHeaderBodySeparatorDefect |
Guido van Rossum | 8b3febe | 2007-08-30 01:15:14 +0000 | [diff] [blame] | 58 | |
| 59 | class MultipartInvariantViolationDefect(MessageDefect): |
| 60 | """A message claimed to be a multipart but no subparts were found.""" |
R David Murray | 749073a | 2011-06-22 13:47:53 -0400 | [diff] [blame] | 61 | |
| 62 | class InvalidMultipartContentTransferEncodingDefect(MessageDefect): |
| 63 | """An invalid content transfer encoding was set on the multipart itself.""" |
R David Murray | 0b6f6c8 | 2012-05-25 18:42:14 -0400 | [diff] [blame] | 64 | |
| 65 | class UndecodableBytesDefect(MessageDefect): |
| 66 | """Header contained bytes that could not be decoded""" |
| 67 | |
| 68 | class InvalidBase64PaddingDefect(MessageDefect): |
| 69 | """base64 encoded sequence had an incorrect length""" |
| 70 | |
| 71 | class InvalidBase64CharactersDefect(MessageDefect): |
| 72 | """base64 encoded sequence had characters not in base64 alphabet""" |
| 73 | |
| 74 | # These errors are specific to header parsing. |
| 75 | |
| 76 | class HeaderDefect(MessageDefect): |
| 77 | """Base class for a header defect.""" |
| 78 | |
| 79 | class InvalidHeaderDefect(HeaderDefect): |
| 80 | """Header is not valid, message gives details.""" |
| 81 | |
| 82 | class HeaderMissingRequiredValue(HeaderDefect): |
| 83 | """A header that must have a value had none""" |
| 84 | |
| 85 | class NonPrintableDefect(HeaderDefect): |
| 86 | """ASCII characters outside the ascii-printable range found""" |
| 87 | |
| 88 | def __init__(self, non_printables): |
| 89 | super().__init__(non_printables) |
| 90 | self.non_printables = non_printables |
| 91 | |
| 92 | def __str__(self): |
| 93 | return ("the following ASCII non-printables found in header: " |
| 94 | "{}".format(self.non_printables)) |
| 95 | |
| 96 | class ObsoleteHeaderDefect(HeaderDefect): |
| 97 | """Header uses syntax declared obsolete by RFC 5322""" |
| 98 | |
| 99 | class NonASCIILocalPartDefect(HeaderDefect): |
| 100 | """local_part contains non-ASCII characters""" |
| 101 | # This defect only occurs during unicode parsing, not when |
| 102 | # parsing messages decoded from binary. |