blob: 294e3edbdcf2c0532f6632367ed16ab235262320 [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
7
8The following exception classes are defined in the :mod:`email.errors` module:
9
10
11.. exception:: MessageError()
12
13 This is the base class for all exceptions that the :mod:`email` package can
14 raise. It is derived from the standard :exc:`Exception` class and defines no
15 additional methods.
16
17
18.. exception:: MessageParseError()
19
Georg Brandl7cb13192010-08-03 12:06:29 +000020 This is the base class for exceptions raised by the :class:`~email.parser.Parser`
Georg Brandl3638e482009-04-27 16:46:17 +000021 class. It is derived from :exc:`MessageError`.
Georg Brandl116aa622007-08-15 14:28:22 +000022
23
24.. exception:: HeaderParseError()
25
26 Raised under some error conditions when parsing the :rfc:`2822` headers of a
27 message, this class is derived from :exc:`MessageParseError`. It can be raised
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030028 from the :meth:`Parser.parse <email.parser.Parser.parse>` or
29 :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
Georg Brandl116aa622007-08-15 14:28:22 +000030
31 Situations where it can be raised include finding an envelope header after the
32 first :rfc:`2822` header of the message, finding a continuation line before the
33 first :rfc:`2822` header is found, or finding a line in the headers which is
34 neither a header or a continuation line.
35
36
37.. exception:: BoundaryError()
38
39 Raised under some error conditions when parsing the :rfc:`2822` headers of a
40 message, this class is derived from :exc:`MessageParseError`. It can be raised
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030041 from the :meth:`Parser.parse <email.parser.Parser.parse>` or
42 :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
Georg Brandl116aa622007-08-15 14:28:22 +000043
44 Situations where it can be raised include not being able to find the starting or
45 terminating boundary in a :mimetype:`multipart/\*` message when strict parsing
46 is used.
47
48
49.. exception:: MultipartConversionError()
50
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030051 Raised when a payload is added to a :class:`~email.message.Message` object
52 using :meth:`add_payload`, but the payload is already a scalar and the
53 message's :mailheader:`Content-Type` main type is not either
54 :mimetype:`multipart` or missing. :exc:`MultipartConversionError` multiply
55 inherits from :exc:`MessageError` and the built-in :exc:`TypeError`.
Georg Brandl116aa622007-08-15 14:28:22 +000056
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030057 Since :meth:`Message.add_payload` is deprecated, this exception is rarely
58 raised in practice. However the exception may also be raised if the
59 :meth:`~email.message.Message.attach`
Georg Brandl116aa622007-08-15 14:28:22 +000060 method is called on an instance of a class derived from
Georg Brandl3638e482009-04-27 16:46:17 +000061 :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
62 :class:`~email.mime.image.MIMEImage`).
Georg Brandl116aa622007-08-15 14:28:22 +000063
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +030064Here's the list of the defects that the :class:`~email.parser.FeedParser`
Georg Brandl3638e482009-04-27 16:46:17 +000065can find while parsing messages. Note that the defects are added to the message
66where the problem was found, so for example, if a message nested inside a
Georg Brandl116aa622007-08-15 14:28:22 +000067:mimetype:`multipart/alternative` had a malformed header, that nested message
68object would have a defect, but the containing messages would not.
69
70All defect classes are subclassed from :class:`email.errors.MessageDefect`, but
71this class is *not* an exception!
72
Georg Brandl116aa622007-08-15 14:28:22 +000073* :class:`NoBoundaryInMultipartDefect` -- A message claimed to be a multipart,
74 but had no :mimetype:`boundary` parameter.
75
76* :class:`StartBoundaryNotFoundDefect` -- The start boundary claimed in the
77 :mailheader:`Content-Type` header was never found.
78
R David Murray7ef3ff32012-05-27 22:20:42 -040079* :class:`CloseBoundaryNotFoundDefect` -- A start boundary was found, but
80 no corresponding close boundary was ever found.
81
Georg Brandl61063cc2012-06-24 22:48:30 +020082 .. versionadded:: 3.3
R David Murray7ef3ff32012-05-27 22:20:42 -040083
Georg Brandl116aa622007-08-15 14:28:22 +000084* :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation
85 line as its first header line.
86
87* :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the
88 middle of a header block.
89
R David Murrayadbdcdb2012-05-27 20:45:01 -040090* :class:`MissingHeaderBodySeparatorDefect` - A line was found while parsing
91 headers that had no leading white space but contained no ':'. Parsing
92 continues assuming that the line represents the first line of the body.
93
Georg Brandl61063cc2012-06-24 22:48:30 +020094 .. versionadded:: 3.3
R David Murrayadbdcdb2012-05-27 20:45:01 -040095
Georg Brandl116aa622007-08-15 14:28:22 +000096* :class:`MalformedHeaderDefect` -- A header was found that was missing a colon,
97 or was otherwise malformed.
98
R David Murrayadbdcdb2012-05-27 20:45:01 -040099 .. deprecated:: 3.3
100 This defect has not been used for several Python versions.
101
Georg Brandl116aa622007-08-15 14:28:22 +0000102* :class:`MultipartInvariantViolationDefect` -- A message claimed to be a
Serhiy Storchakae0f0cf42013-08-19 09:59:18 +0300103 :mimetype:`multipart`, but no subparts were found. Note that when a message
104 has this defect, its :meth:`~email.message.Message.is_multipart` method may
105 return false even though its content type claims to be :mimetype:`multipart`.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
R David Murray80e0aee2012-05-27 21:23:34 -0400107* :class:`InvalidBase64PaddingDefect` -- When decoding a block of base64
108 enocded bytes, the padding was not correct. Enough padding is added to
109 perform the decode, but the resulting decoded bytes may be invalid.
110
111* :class:`InvalidBase64CharactersDefect` -- When decoding a block of base64
112 enocded bytes, characters outside the base64 alphebet were encountered.
113 The characters are ignored, but the resulting decoded bytes may be invalid.