/Doc/library/email.errors.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 92 lines · 61 code · 31 blank · 0 comment · 0 complexity · d9009b70837adf354c13cfeb4a3a8ea9 MD5 · raw file

  1. :mod:`email`: Exception and Defect classes
  2. ------------------------------------------
  3. .. module:: email.errors
  4. :synopsis: The exception classes used by the email package.
  5. The following exception classes are defined in the :mod:`email.errors` module:
  6. .. exception:: MessageError()
  7. This is the base class for all exceptions that the :mod:`email` package can
  8. raise. It is derived from the standard :exc:`Exception` class and defines no
  9. additional methods.
  10. .. exception:: MessageParseError()
  11. This is the base class for exceptions thrown by the :class:`~email.parser.Parser`
  12. class. It is derived from :exc:`MessageError`.
  13. .. exception:: HeaderParseError()
  14. Raised under some error conditions when parsing the :rfc:`2822` headers of a
  15. message, this class is derived from :exc:`MessageParseError`. It can be raised
  16. from the :meth:`Parser.parse` or :meth:`Parser.parsestr` methods.
  17. Situations where it can be raised include finding an envelope header after the
  18. first :rfc:`2822` header of the message, finding a continuation line before the
  19. first :rfc:`2822` header is found, or finding a line in the headers which is
  20. neither a header or a continuation line.
  21. .. exception:: BoundaryError()
  22. Raised under some error conditions when parsing the :rfc:`2822` headers of a
  23. message, this class is derived from :exc:`MessageParseError`. It can be raised
  24. from the :meth:`Parser.parse` or :meth:`Parser.parsestr` methods.
  25. Situations where it can be raised include not being able to find the starting or
  26. terminating boundary in a :mimetype:`multipart/\*` message when strict parsing
  27. is used.
  28. .. exception:: MultipartConversionError()
  29. Raised when a payload is added to a :class:`Message` object using
  30. :meth:`add_payload`, but the payload is already a scalar and the message's
  31. :mailheader:`Content-Type` main type is not either :mimetype:`multipart` or
  32. missing. :exc:`MultipartConversionError` multiply inherits from
  33. :exc:`MessageError` and the built-in :exc:`TypeError`.
  34. Since :meth:`Message.add_payload` is deprecated, this exception is rarely raised
  35. in practice. However the exception may also be raised if the :meth:`attach`
  36. method is called on an instance of a class derived from
  37. :class:`~email.mime.nonmultipart.MIMENonMultipart` (e.g.
  38. :class:`~email.mime.image.MIMEImage`).
  39. Here's the list of the defects that the :class:`~email.mime.parser.FeedParser`
  40. can find while parsing messages. Note that the defects are added to the message
  41. where the problem was found, so for example, if a message nested inside a
  42. :mimetype:`multipart/alternative` had a malformed header, that nested message
  43. object would have a defect, but the containing messages would not.
  44. All defect classes are subclassed from :class:`email.errors.MessageDefect`, but
  45. this class is *not* an exception!
  46. .. versionadded:: 2.4
  47. All the defect classes were added.
  48. * :class:`NoBoundaryInMultipartDefect` -- A message claimed to be a multipart,
  49. but had no :mimetype:`boundary` parameter.
  50. * :class:`StartBoundaryNotFoundDefect` -- The start boundary claimed in the
  51. :mailheader:`Content-Type` header was never found.
  52. * :class:`FirstHeaderLineIsContinuationDefect` -- The message had a continuation
  53. line as its first header line.
  54. * :class:`MisplacedEnvelopeHeaderDefect` - A "Unix From" header was found in the
  55. middle of a header block.
  56. * :class:`MalformedHeaderDefect` -- A header was found that was missing a colon,
  57. or was otherwise malformed.
  58. * :class:`MultipartInvariantViolationDefect` -- A message claimed to be a
  59. :mimetype:`multipart`, but no subparts were found. Note that when a message has
  60. this defect, its :meth:`is_multipart` method may return false even though its
  61. content type claims to be :mimetype:`multipart`.