blob: 511ad16358319796a3940064e504e3668e61bc4d [file] [log] [blame]
R David Murray79cf3ba2012-05-27 17:10:36 -04001:mod:`email.errors`: Exception and Defect classes
2-------------------------------------------------
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: email.errors
5 :synopsis: The exception classes used by the email package.
6
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/email/errors.py`
8
9--------------
Georg Brandl116aa622007-08-15 14:28:22 +000010
11The 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
R David Murray29d1bc02016-09-07 21:15:59 -040023 This is the base class for exceptions raised by the
24 :class:`~email.parser.Parser` class. It is derived from
25 :exc:`MessageError`. This class is also used internally by the parser used
26 by :mod:`~email.headerregistry`.
Georg Brandl116aa622007-08-15 14:28:22 +000027
28
29.. exception:: HeaderParseError()
30
R David Murray29d1bc02016-09-07 21:15:59 -040031 Raised under some error conditions when parsing the :rfc:`5322` headers of a
32 message, this class is derived from :exc:`MessageParseError`. The
33 :meth:`~email.message.EmailMessage.set_boundary` method will raise this
34 error if the content type is unknown when the method is called.
35 :class:`~email.header.Header` may raise this error for certain base64
36 decoding errors, and when an attempt is made to create a header that appears
37 to contain an embedded header (that is, there is what is supposed to be a
38 continuation line that has no leading whitespace and looks like a header).
Georg Brandl116aa622007-08-15 14:28:22 +000039
40
41.. exception:: BoundaryError()
42
R David Murray29d1bc02016-09-07 21:15:59 -040043 Deprecated and no longer used.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
46.. exception:: MultipartConversionError()
47
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030048 Raised when a payload is added to a :class:`~email.message.Message` object
49 using :meth:`add_payload`, but the payload is already a scalar and the
50 message's :mailheader:`Content-Type` main type is not either
51 :mimetype:`multipart` or missing. :exc:`MultipartConversionError` multiply
52 inherits from :exc:`MessageError` and the built-in :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +000053
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030054 Since :meth:`Message.add_payload` is deprecated, this exception is rarely
55 raised in practice. However the exception may also be raised if the
56 :meth:`~email.message.Message.attach`
Georg Brandl116aa622007-08-15 14:28:22 +000057 method is called on an instance of a class derived from
Georg Brandl3638e482009-04-27 16:46:17 +000058 :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
59 :class:`~email.mime.image.MIMEImage`).
Georg Brandl116aa622007-08-15 14:28:22 +000060
R David Murray29d1bc02016-09-07 21:15:59 -040061
62Here is the list of the defects that the :class:`~email.parser.FeedParser`
Georg Brandl3638e482009-04-27 16:46:17 +000063can find while parsing messages. Note that the defects are added to the message
64where the problem was found, so for example, if a message nested inside a
Georg Brandl116aa622007-08-15 14:28:22 +000065:mimetype:`multipart/alternative` had a malformed header, that nested message
66object would have a defect, but the containing messages would not.
67
R David Murray29d1bc02016-09-07 21:15:59 -040068All defect classes are subclassed from :class:`email.errors.MessageDefect`.
Georg Brandl116aa622007-08-15 14:28:22 +000069
Georg Brandl116aa622007-08-15 14:28:22 +000070* :class:`NoBoundaryInMultipartDefect` -- A message claimed to be a multipart,
71 but had no :mimetype:`boundary` parameter.
72
73* :class:`StartBoundaryNotFoundDefect` -- The start boundary claimed in the
74 :mailheader:`Content-Type` header was never found.
75
R David Murray7ef3ff32012-05-27 22:20:42 -040076* :class:`CloseBoundaryNotFoundDefect` -- A start boundary was found, but
77 no corresponding close boundary was ever found.
78
Georg Brandl61063cc2012-06-24 22:48:30 +020079 .. versionadded:: 3.3
R David Murray7ef3ff32012-05-27 22:20:42 -040080
Georg Brandl116aa622007-08-15 14:28:22 +000081* :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation
82 line as its first header line.
83
84* :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the
85 middle of a header block.
86
R David Murrayadbdcdb2012-05-27 20:45:01 -040087* :class:`MissingHeaderBodySeparatorDefect` - A line was found while parsing
88 headers that had no leading white space but contained no ':'. Parsing
89 continues assuming that the line represents the first line of the body.
90
Georg Brandl61063cc2012-06-24 22:48:30 +020091 .. versionadded:: 3.3
R David Murrayadbdcdb2012-05-27 20:45:01 -040092
Georg Brandl116aa622007-08-15 14:28:22 +000093* :class:`MalformedHeaderDefect` -- A header was found that was missing a colon,
94 or was otherwise malformed.
95
R David Murrayadbdcdb2012-05-27 20:45:01 -040096 .. deprecated:: 3.3
97 This defect has not been used for several Python versions.
98
Georg Brandl116aa622007-08-15 14:28:22 +000099* :class:`MultipartInvariantViolationDefect` -- A message claimed to be a
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300100 :mimetype:`multipart`, but no subparts were found. Note that when a message
101 has this defect, its :meth:`~email.message.Message.is_multipart` method may
102 return false even though its content type claims to be :mimetype:`multipart`.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
R David Murray80e0aee2012-05-27 21:23:34 -0400104* :class:`InvalidBase64PaddingDefect` -- When decoding a block of base64
delirious-lettuce3378b202017-05-19 14:37:57 -0600105 encoded bytes, the padding was not correct. Enough padding is added to
R David Murray80e0aee2012-05-27 21:23:34 -0400106 perform the decode, but the resulting decoded bytes may be invalid.
107
108* :class:`InvalidBase64CharactersDefect` -- When decoding a block of base64
delirious-lettuce3378b202017-05-19 14:37:57 -0600109 encoded bytes, characters outside the base64 alphabet were encountered.
R David Murray80e0aee2012-05-27 21:23:34 -0400110 The characters are ignored, but the resulting decoded bytes may be invalid.
Tal Einatc3f55be2018-06-12 15:46:22 +0300111
112* :class:`InvalidBase64LengthDefect` -- When decoding a block of base64 encoded
113 bytes, the number of non-padding base64 characters was invalid (1 more than
114 a multiple of 4). The encoded block was kept as-is.