blob: 499d754fa0895888425dd725a8c561a115bd9f6e [file] [log] [blame]
R David Murrayb42b6eb2012-05-27 17:13:54 -04001:mod:`email.errors`: Exception and Defect classes
2-------------------------------------------------
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Brandl21946af2010-10-06 09:28:45 +000020 This is the base class for exceptions raised by the :class:`~email.parser.Parser`
Georg Brandlb48327a2009-04-13 13:13:25 +000021 class. It is derived from :exc:`MessageError`.
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Storchakaf65d4542013-08-19 10:03:25 +030028 from the :meth:`Parser.parse <email.parser.Parser.parse>` or
29 :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Storchakaf65d4542013-08-19 10:03:25 +030041 from the :meth:`Parser.parse <email.parser.Parser.parse>` or
42 :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +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 Storchakaf65d4542013-08-19 10:03:25 +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 Brandl8ec7f652007-08-15 14:28:01 +000056
Serhiy Storchakaf65d4542013-08-19 10:03:25 +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 Brandl8ec7f652007-08-15 14:28:01 +000060 method is called on an instance of a class derived from
Georg Brandlb48327a2009-04-13 13:13:25 +000061 :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
62 :class:`~email.mime.image.MIMEImage`).
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
Serhiy Storchakaf65d4542013-08-19 10:03:25 +030064Here's the list of the defects that the :class:`~email.parser.FeedParser`
Georg Brandlb48327a2009-04-13 13:13:25 +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 Brandl8ec7f652007-08-15 14:28:01 +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
73.. versionadded:: 2.4
74 All the defect classes were added.
75
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
82* :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation
83 line as its first header line.
84
85* :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the
86 middle of a header block.
87
88* :class:`MalformedHeaderDefect` -- A header was found that was missing a colon,
89 or was otherwise malformed.
90
91* :class:`MultipartInvariantViolationDefect` -- A message claimed to be a
Serhiy Storchakaf65d4542013-08-19 10:03:25 +030092 :mimetype:`multipart`, but no subparts were found. Note that when a message
93 has this defect, its :meth:`~email.message.Message.is_multipart` method may
94 return false even though its content type claims to be :mimetype:`multipart`.
Georg Brandl8ec7f652007-08-15 14:28:01 +000095