R David Murray | 79cf3ba | 2012-05-27 17:10:36 -0400 | [diff] [blame] | 1 | :mod:`email.errors`: Exception and Defect classes |
| 2 | ------------------------------------------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: email.errors |
| 5 | :synopsis: The exception classes used by the email package. |
| 6 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | **Source code:** :source:`Lib/email/errors.py` |
| 8 | |
| 9 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 10 | |
| 11 | The following exception classes are defined in the :mod:`email.errors` module: |
| 12 | |
| 13 | |
| 14 | .. exception:: MessageError() |
| 15 | |
| 16 | This is the base class for all exceptions that the :mod:`email` package can |
| 17 | raise. It is derived from the standard :exc:`Exception` class and defines no |
| 18 | additional methods. |
| 19 | |
| 20 | |
| 21 | .. exception:: MessageParseError() |
| 22 | |
Georg Brandl | 7cb1319 | 2010-08-03 12:06:29 +0000 | [diff] [blame] | 23 | This is the base class for exceptions raised by the :class:`~email.parser.Parser` |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 24 | class. It is derived from :exc:`MessageError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | |
| 27 | .. exception:: HeaderParseError() |
| 28 | |
| 29 | Raised under some error conditions when parsing the :rfc:`2822` headers of a |
| 30 | message, this class is derived from :exc:`MessageParseError`. It can be raised |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 31 | from the :meth:`Parser.parse <email.parser.Parser.parse>` or |
| 32 | :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
| 34 | Situations where it can be raised include finding an envelope header after the |
| 35 | first :rfc:`2822` header of the message, finding a continuation line before the |
| 36 | first :rfc:`2822` header is found, or finding a line in the headers which is |
| 37 | neither a header or a continuation line. |
| 38 | |
| 39 | |
| 40 | .. exception:: BoundaryError() |
| 41 | |
| 42 | Raised under some error conditions when parsing the :rfc:`2822` headers of a |
| 43 | message, this class is derived from :exc:`MessageParseError`. It can be raised |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 44 | from the :meth:`Parser.parse <email.parser.Parser.parse>` or |
| 45 | :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 46 | |
| 47 | Situations where it can be raised include not being able to find the starting or |
| 48 | terminating boundary in a :mimetype:`multipart/\*` message when strict parsing |
| 49 | is used. |
| 50 | |
| 51 | |
| 52 | .. exception:: MultipartConversionError() |
| 53 | |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 54 | Raised when a payload is added to a :class:`~email.message.Message` object |
| 55 | using :meth:`add_payload`, but the payload is already a scalar and the |
| 56 | message's :mailheader:`Content-Type` main type is not either |
| 57 | :mimetype:`multipart` or missing. :exc:`MultipartConversionError` multiply |
| 58 | inherits from :exc:`MessageError` and the built-in :exc:`TypeError`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 60 | Since :meth:`Message.add_payload` is deprecated, this exception is rarely |
| 61 | raised in practice. However the exception may also be raised if the |
| 62 | :meth:`~email.message.Message.attach` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | method is called on an instance of a class derived from |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 64 | :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g. |
| 65 | :class:`~email.mime.image.MIMEImage`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 66 | |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 67 | Here's the list of the defects that the :class:`~email.parser.FeedParser` |
Georg Brandl | 3638e48 | 2009-04-27 16:46:17 +0000 | [diff] [blame] | 68 | can find while parsing messages. Note that the defects are added to the message |
| 69 | where the problem was found, so for example, if a message nested inside a |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | :mimetype:`multipart/alternative` had a malformed header, that nested message |
| 71 | object would have a defect, but the containing messages would not. |
| 72 | |
| 73 | All defect classes are subclassed from :class:`email.errors.MessageDefect`, but |
| 74 | this class is *not* an exception! |
| 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | * :class:`NoBoundaryInMultipartDefect` -- A message claimed to be a multipart, |
| 77 | but had no :mimetype:`boundary` parameter. |
| 78 | |
| 79 | * :class:`StartBoundaryNotFoundDefect` -- The start boundary claimed in the |
| 80 | :mailheader:`Content-Type` header was never found. |
| 81 | |
R David Murray | 7ef3ff3 | 2012-05-27 22:20:42 -0400 | [diff] [blame] | 82 | * :class:`CloseBoundaryNotFoundDefect` -- A start boundary was found, but |
| 83 | no corresponding close boundary was ever found. |
| 84 | |
Georg Brandl | 61063cc | 2012-06-24 22:48:30 +0200 | [diff] [blame] | 85 | .. versionadded:: 3.3 |
R David Murray | 7ef3ff3 | 2012-05-27 22:20:42 -0400 | [diff] [blame] | 86 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 87 | * :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation |
| 88 | line as its first header line. |
| 89 | |
| 90 | * :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the |
| 91 | middle of a header block. |
| 92 | |
R David Murray | adbdcdb | 2012-05-27 20:45:01 -0400 | [diff] [blame] | 93 | * :class:`MissingHeaderBodySeparatorDefect` - A line was found while parsing |
| 94 | headers that had no leading white space but contained no ':'. Parsing |
| 95 | continues assuming that the line represents the first line of the body. |
| 96 | |
Georg Brandl | 61063cc | 2012-06-24 22:48:30 +0200 | [diff] [blame] | 97 | .. versionadded:: 3.3 |
R David Murray | adbdcdb | 2012-05-27 20:45:01 -0400 | [diff] [blame] | 98 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 99 | * :class:`MalformedHeaderDefect` -- A header was found that was missing a colon, |
| 100 | or was otherwise malformed. |
| 101 | |
R David Murray | adbdcdb | 2012-05-27 20:45:01 -0400 | [diff] [blame] | 102 | .. deprecated:: 3.3 |
| 103 | This defect has not been used for several Python versions. |
| 104 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 | * :class:`MultipartInvariantViolationDefect` -- A message claimed to be a |
Serhiy Storchaka | e0f0cf4 | 2013-08-19 09:59:18 +0300 | [diff] [blame] | 106 | :mimetype:`multipart`, but no subparts were found. Note that when a message |
| 107 | has this defect, its :meth:`~email.message.Message.is_multipart` method may |
| 108 | return false even though its content type claims to be :mimetype:`multipart`. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 109 | |
R David Murray | 80e0aee | 2012-05-27 21:23:34 -0400 | [diff] [blame] | 110 | * :class:`InvalidBase64PaddingDefect` -- When decoding a block of base64 |
| 111 | enocded bytes, the padding was not correct. Enough padding is added to |
| 112 | perform the decode, but the resulting decoded bytes may be invalid. |
| 113 | |
| 114 | * :class:`InvalidBase64CharactersDefect` -- When decoding a block of base64 |
| 115 | enocded bytes, characters outside the base64 alphebet were encountered. |
| 116 | The characters are ignored, but the resulting decoded bytes may be invalid. |