• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1:mod:`!email.errors`: Exception and Defect classes
2--------------------------------------------------
3
4.. module:: email.errors
5   :synopsis: The exception classes used by the email package.
6
7**Source code:** :source:`Lib/email/errors.py`
8
9--------------
10
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
23   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`.
27
28
29.. exception:: HeaderParseError()
30
31   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).
39
40
41.. exception:: BoundaryError()
42
43   Deprecated and no longer used.
44
45
46.. exception:: MultipartConversionError()
47
48   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`.
53
54   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`
57   method is called on an instance of a class derived from
58   :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
59   :class:`~email.mime.image.MIMEImage`).
60
61
62.. exception:: HeaderWriteError()
63
64   Raised when an error occurs when the :mod:`~email.generator` outputs
65   headers.
66
67
68.. exception:: MessageDefect()
69
70   This is the base class for all defects found when parsing email messages.
71   It is derived from :exc:`ValueError`.
72
73.. exception:: HeaderDefect()
74
75   This is the base class for all defects found when parsing email headers.
76   It is derived from :exc:`MessageDefect`.
77
78Here is the list of the defects that the :class:`~email.parser.FeedParser`
79can find while parsing messages.  Note that the defects are added to the message
80where the problem was found, so for example, if a message nested inside a
81:mimetype:`multipart/alternative` had a malformed header, that nested message
82object would have a defect, but the containing messages would not.
83
84All defect classes are subclassed from :class:`email.errors.MessageDefect`.
85
86* :class:`NoBoundaryInMultipartDefect` -- A message claimed to be a multipart,
87  but had no :mimetype:`boundary` parameter.
88
89* :class:`StartBoundaryNotFoundDefect` -- The start boundary claimed in the
90  :mailheader:`Content-Type` header was never found.
91
92* :class:`CloseBoundaryNotFoundDefect` -- A start boundary was found, but
93  no corresponding close boundary was ever found.
94
95  .. versionadded:: 3.3
96
97* :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation
98  line as its first header line.
99
100* :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the
101  middle of a header block.
102
103* :class:`MissingHeaderBodySeparatorDefect` - A line was found while parsing
104  headers that had no leading white space but contained no ':'.  Parsing
105  continues assuming that the line represents the first line of the body.
106
107  .. versionadded:: 3.3
108
109* :class:`MalformedHeaderDefect` -- A header was found that was missing a colon,
110  or was otherwise malformed.
111
112  .. deprecated:: 3.3
113     This defect has not been used for several Python versions.
114
115* :class:`MultipartInvariantViolationDefect` -- A message claimed to be a
116  :mimetype:`multipart`, but no subparts were found.  Note that when a message
117  has this defect, its :meth:`~email.message.Message.is_multipart` method may
118  return ``False`` even though its content type claims to be :mimetype:`multipart`.
119
120* :class:`InvalidBase64PaddingDefect` -- When decoding a block of base64
121  encoded bytes, the padding was not correct.  Enough padding is added to
122  perform the decode, but the resulting decoded bytes may be invalid.
123
124* :class:`InvalidBase64CharactersDefect` -- When decoding a block of base64
125  encoded bytes, characters outside the base64 alphabet were encountered.
126  The characters are ignored, but the resulting decoded bytes may be invalid.
127
128* :class:`InvalidBase64LengthDefect` -- When decoding a block of base64 encoded
129  bytes, the number of non-padding base64 characters was invalid (1 more than
130  a multiple of 4).  The encoded block was kept as-is.
131
132* :class:`InvalidDateDefect` -- When decoding an invalid or unparsable date field.
133  The original value is kept as-is.
134