errors.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # Copyright (C) 2001-2006 Python Software Foundation
  2. # Author: Barry Warsaw
  3. # Contact: email-sig@python.org
  4. """email package exception classes."""
  5. from __future__ import unicode_literals
  6. from __future__ import division
  7. from __future__ import absolute_import
  8. from future.builtins import super
  9. class MessageError(Exception):
  10. """Base class for errors in the email package."""
  11. class MessageParseError(MessageError):
  12. """Base class for message parsing errors."""
  13. class HeaderParseError(MessageParseError):
  14. """Error while parsing headers."""
  15. class BoundaryError(MessageParseError):
  16. """Couldn't find terminating boundary."""
  17. class MultipartConversionError(MessageError, TypeError):
  18. """Conversion to a multipart is prohibited."""
  19. class CharsetError(MessageError):
  20. """An illegal charset was given."""
  21. # These are parsing defects which the parser was able to work around.
  22. class MessageDefect(ValueError):
  23. """Base class for a message defect."""
  24. def __init__(self, line=None):
  25. if line is not None:
  26. super().__init__(line)
  27. self.line = line
  28. class NoBoundaryInMultipartDefect(MessageDefect):
  29. """A message claimed to be a multipart but had no boundary parameter."""
  30. class StartBoundaryNotFoundDefect(MessageDefect):
  31. """The claimed start boundary was never found."""
  32. class CloseBoundaryNotFoundDefect(MessageDefect):
  33. """A start boundary was found, but not the corresponding close boundary."""
  34. class FirstHeaderLineIsContinuationDefect(MessageDefect):
  35. """A message had a continuation line as its first header line."""
  36. class MisplacedEnvelopeHeaderDefect(MessageDefect):
  37. """A 'Unix-from' header was found in the middle of a header block."""
  38. class MissingHeaderBodySeparatorDefect(MessageDefect):
  39. """Found line with no leading whitespace and no colon before blank line."""
  40. # XXX: backward compatibility, just in case (it was never emitted).
  41. MalformedHeaderDefect = MissingHeaderBodySeparatorDefect
  42. class MultipartInvariantViolationDefect(MessageDefect):
  43. """A message claimed to be a multipart but no subparts were found."""
  44. class InvalidMultipartContentTransferEncodingDefect(MessageDefect):
  45. """An invalid content transfer encoding was set on the multipart itself."""
  46. class UndecodableBytesDefect(MessageDefect):
  47. """Header contained bytes that could not be decoded"""
  48. class InvalidBase64PaddingDefect(MessageDefect):
  49. """base64 encoded sequence had an incorrect length"""
  50. class InvalidBase64CharactersDefect(MessageDefect):
  51. """base64 encoded sequence had characters not in base64 alphabet"""
  52. # These errors are specific to header parsing.
  53. class HeaderDefect(MessageDefect):
  54. """Base class for a header defect."""
  55. def __init__(self, *args, **kw):
  56. super().__init__(*args, **kw)
  57. class InvalidHeaderDefect(HeaderDefect):
  58. """Header is not valid, message gives details."""
  59. class HeaderMissingRequiredValue(HeaderDefect):
  60. """A header that must have a value had none"""
  61. class NonPrintableDefect(HeaderDefect):
  62. """ASCII characters outside the ascii-printable range found"""
  63. def __init__(self, non_printables):
  64. super().__init__(non_printables)
  65. self.non_printables = non_printables
  66. def __str__(self):
  67. return ("the following ASCII non-printables found in header: "
  68. "{}".format(self.non_printables))
  69. class ObsoleteHeaderDefect(HeaderDefect):
  70. """Header uses syntax declared obsolete by RFC 5322"""
  71. class NonASCIILocalPartDefect(HeaderDefect):
  72. """local_part contains non-ASCII characters"""
  73. # This defect only occurs during unicode parsing, not when
  74. # parsing messages decoded from binary.