跳转到主要内容

为Python异常提供错误消息,即使原始消息为空

项目描述

错误文本

错误文本是一个Python包,用于为Python异常提供错误消息,即使原始消息为空。

以下是一个可能以不同方式失败的功能的示例

from errortext import error_text

def do_something(action_to_perform):
    if action_to_perform == "walk":
        print("walking")
    elif action_to_perform == "sleep":
        raise ValueError("must be tired in order to sleep")
    elif action_to_perform == "run":
        raise NotImplementedError("run")
    else:
        assert False

可以使用以下代码运行此函数,并通过使用异常的str(error)错误消息打印可能的错误详情

try:
    do_something(action)
    print(f"Success: done with {action}")
except Exception as error:
    print(f"Error: cannot {action}: {str(error)}")

如您所见,某些错误可能没有任何有意义的消息

do_something("sleep") -> cannot sleep: must be tired in order to sleep
do_something("run")   -> cannot run: run
do_something("xxx")   -> cannot xxx:

将错误处理器更改为

print(f"Error with error_text: cannot {action}: {error_text(error)}")

错误消息至少将包含一些关于发生什么错误的提示

do_something("sleep") -> cannot sleep: must be tired in order to sleep
do_something("run")   -> cannot run: NotImplementedError: run
do_something("xxx")   -> cannot xxx: AssertionError

项目详情


下载文件

下载适用于您平台文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。

源代码分发

本发布版本没有提供源代码分发文件。请参阅有关 生成分发存档 的教程。

构建分发

errortext-0.1.0-py3-none-any.whl (3.8 kB 查看哈希值)

上传于 Python 3

由...支持