errors.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. class HeaderWriteError(MessageError):
  18. """Error while writing headers."""
  19. # These are parsing defects which the parser was able to work around.
  20. class MessageDefect(ValueError):
  21. """Base class for a message defect."""
  22. def __init__(self, line=None):
  23. if line is not None:
  24. super().__init__(line)
  25. self.line = line
  26. class NoBoundaryInMultipartDefect(MessageDefect):
  27. """A message claimed to be a multipart but had no boundary parameter."""
  28. class StartBoundaryNotFoundDefect(MessageDefect):
  29. """The claimed start boundary was never found."""
  30. class CloseBoundaryNotFoundDefect(MessageDefect):
  31. """A start boundary was found, but not the corresponding close boundary."""
  32. class FirstHeaderLineIsContinuationDefect(MessageDefect):
  33. """A message had a continuation line as its first header line."""
  34. class MisplacedEnvelopeHeaderDefect(MessageDefect):
  35. """A 'Unix-from' header was found in the middle of a header block."""
  36. class MissingHeaderBodySeparatorDefect(MessageDefect):
  37. """Found line with no leading whitespace and no colon before blank line."""
  38. # XXX: backward compatibility, just in case (it was never emitted).
  39. MalformedHeaderDefect = MissingHeaderBodySeparatorDefect
  40. class MultipartInvariantViolationDefect(MessageDefect):
  41. """A message claimed to be a multipart but no subparts were found."""
  42. class InvalidMultipartContentTransferEncodingDefect(MessageDefect):
  43. """An invalid content transfer encoding was set on the multipart itself."""
  44. class UndecodableBytesDefect(MessageDefect):
  45. """Header contained bytes that could not be decoded"""
  46. class InvalidBase64PaddingDefect(MessageDefect):
  47. """base64 encoded sequence had an incorrect length"""
  48. class InvalidBase64CharactersDefect(MessageDefect):
  49. """base64 encoded sequence had characters not in base64 alphabet"""
  50. class InvalidBase64LengthDefect(MessageDefect):
  51. """base64 encoded sequence had invalid length (1 mod 4)"""
  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.
  75. class InvalidDateDefect(HeaderDefect):
  76. """Header has unparsable or invalid date"""