Home | History | Annotate | Download | only in library
      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 
      8 The 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 
     20    This is the base class for exceptions raised by the :class:`~email.parser.Parser`
     21    class.  It is derived from :exc:`MessageError`.
     22 
     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
     28    from the :meth:`Parser.parse <email.parser.Parser.parse>` or
     29    :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
     30 
     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
     41    from the :meth:`Parser.parse <email.parser.Parser.parse>` or
     42    :meth:`Parser.parsestr <email.parser.Parser.parsestr>` methods.
     43 
     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 
     51    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`.
     56 
     57    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`
     60    method is called on an instance of a class derived from
     61    :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
     62    :class:`~email.mime.image.MIMEImage`).
     63 
     64 Here's the list of the defects that the :class:`~email.parser.FeedParser`
     65 can find while parsing messages.  Note that the defects are added to the message
     66 where the problem was found, so for example, if a message nested inside a
     67 :mimetype:`multipart/alternative` had a malformed header, that nested message
     68 object would have a defect, but the containing messages would not.
     69 
     70 All defect classes are subclassed from :class:`email.errors.MessageDefect`, but
     71 this 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
     92   :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`.
     95 
     96