blob: 8470783e816bfdf801199416a949f60ad645977e [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
Georg Brandl7cb13192010-08-03 12:06:29 +000023 This is the base class for exceptions raised by the :class:`~email.parser.Parser`
Georg Brandl3638e482009-04-27 16:46:17 +000024 class. It is derived from :exc:`MessageError`.
Georg Brandl116aa622007-08-15 14:28:22 +000025
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 Storchakae0f0cf42013-08-19 09:59:18 +030031 from the :meth:`Parser.parse <email.parser.Parser.parse>` or
32 :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
Georg Brandl116aa622007-08-15 14:28:22 +000033
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 Storchakae0f0cf42013-08-19 09:59:18 +030044 from the :meth:`Parser.parse <email.parser.Parser.parse>` or
45 :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
Georg Brandl116aa622007-08-15 14:28:22 +000046
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 Storchakae0f0cf42013-08-19 09:59:18 +030054 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 Brandl116aa622007-08-15 14:28:22 +000059
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030060 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 Brandl116aa622007-08-15 14:28:22 +000063 method is called on an instance of a class derived from
Georg Brandl3638e482009-04-27 16:46:17 +000064 :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
65 :class:`~email.mime.image.MIMEImage`).
Georg Brandl116aa622007-08-15 14:28:22 +000066
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030067Here's the list of the defects that the :class:`~email.parser.FeedParser`
Georg Brandl3638e482009-04-27 16:46:17 +000068can find while parsing messages. Note that the defects are added to the message
69where the problem was found, so for example, if a message nested inside a
Georg Brandl116aa622007-08-15 14:28:22 +000070:mimetype:`multipart/alternative` had a malformed header, that nested message
71object would have a defect, but the containing messages would not.
72
73All defect classes are subclassed from :class:`email.errors.MessageDefect`, but
74this class is *not* an exception!
75
Georg Brandl116aa622007-08-15 14:28:22 +000076* :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 Murray7ef3ff32012-05-27 22:20:42 -040082* :class:`CloseBoundaryNotFoundDefect` -- A start boundary was found, but
83 no corresponding close boundary was ever found.
84
Georg Brandl61063cc2012-06-24 22:48:30 +020085 .. versionadded:: 3.3
R David Murray7ef3ff32012-05-27 22:20:42 -040086
Georg Brandl116aa622007-08-15 14:28:22 +000087* :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 Murrayadbdcdb2012-05-27 20:45:01 -040093* :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 Brandl61063cc2012-06-24 22:48:30 +020097 .. versionadded:: 3.3
R David Murrayadbdcdb2012-05-27 20:45:01 -040098
Georg Brandl116aa622007-08-15 14:28:22 +000099* :class:`MalformedHeaderDefect` -- A header was found that was missing a colon,
100 or was otherwise malformed.
101
R David Murrayadbdcdb2012-05-27 20:45:01 -0400102 .. deprecated:: 3.3
103 This defect has not been used for several Python versions.
104
Georg Brandl116aa622007-08-15 14:28:22 +0000105* :class:`MultipartInvariantViolationDefect` -- A message claimed to be a
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300106 :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 Brandl116aa622007-08-15 14:28:22 +0000109
R David Murray80e0aee2012-05-27 21:23:34 -0400110* :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.