errors.py 3.6 KB

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