19.1.11. email.errors:异常和缺陷类

源代码: Lib / email / errors.py

以下异常类在email.errors模块中定义:

exception email.errors.MessageError

这是email包可引发的所有异常的基类。它是从标准的Exception派生的,并没有定义其他方法。

exception email.errors.MessageParseError

这是由Parser类引发的异常的基类。它源自MessageError

exception email.errors.HeaderParseError

在解析消息的 RFC 2822头时,在某些错误条件下引发,该类从MessageParseError派生。它可以从Parser.parseParser.parsestr方法中提升。

Situations where it can be raised include finding an envelope header after the first RFC 2822 header of the message, finding a continuation line before the first RFC 2822 header is found, or finding a line in the headers which is neither a header or a continuation line.

exception email.errors.BoundaryError

在解析消息的 RFC 2822头时,在某些错误条件下引发,该类从MessageParseError派生。它可以从Parser.parseParser.parsestr方法中提升。

可以引发的情况包括:在使用严格解析时,不能在multipart / *消息中找到开始或终止边界。

exception email.errors.MultipartConversionError

使用add_payload()将有效内容添加到Message对象时引发,但有效内容已是标量,消息的Content-Type main类型不是multipart或缺少。MultipartConversionError multiply继承自MessageError和内建TypeError

由于Message.add_payload()已弃用,因此在实践中很少会出现此异常。然而,如果attach()方法在从MIMENonMultipart派生的类的实例上调用(例如,MIMEImage)。

以下是FeedParser在解析邮件时可以找到的缺陷列表。请注意,缺陷会添加到找到问题的消息中,例如,如果嵌套在multipart / alternative内的消息具有格式错误的标题,那么嵌套消息对象会有缺陷,但是包含消息不会。

所有缺陷类都是email.errors.MessageDefect的子类,但这个类不是异常!

  • NoBoundaryInMultipartDefect - 声明为分段的消息,但没有边界参数。

  • StartBoundaryNotFoundDefect - 从未找到Content-Type头中声明的起始边界。

  • CloseBoundaryNotFoundDefect - 找到了起始边界,但是没有找到对应的边界。

    版本3.3中的新功能。

  • FirstHeaderLineIsContinuationDefect - 邮件具有连续行作为其第一个标题行。

  • MisplacedEnvelopeHeaderDefect - 在标头块的中间找到“Unix From”标头。

  • MissingHeaderBodySeparatorDefect - 解析没有前导空白但不包含“:”的标题时发现了一行。解析继续,假设行表示主体的第一行。

    版本3.3中的新功能。

  • MalformedHeaderDefect - 找到标题,缺少冒号或其他格式错误。

    自版本3.3后已弃用:此缺陷尚未用于多个Python版本。

  • MultipartInvariantViolationDefect - 声明为多部分的消息,但未找到子部件。请注意,当消息具有此缺陷时,即使其is_multipart()方法的内容类型声明为multipart,其方法也可能返回false。

  • InvalidBase64PaddingDefect - 解码一个包含base64字节的块时,填充不正确。添加足够的填充以执行解码,但是所得到的解码字节可能是无效的。

  • InvalidBase64CharactersDefect - 当解码一个包含base64字节的块时,遇到base64 alphebet外的字符。字符被忽略,但是生成的解码字节可能无效。