Python:在关闭资源期间报告错误的最佳做法

问题描述

在 Python 中报告关闭资源时发生的错误的最佳实践是什么?

特别是,如果我实现了 __enter____exit__,我可以使用

with my_library.connect(endpoint) as connection:
   connection.store_data("...")

但是如果关闭连接并保留更改失败,我该怎么办,例如由于网络中断?我知道我可以在技术上从 __exit__ 中引发错误,但这是 Python 中的最佳实践/惯用语吗?或者我应该,例如,提供一个单独的 persistChanges 方法,让 __exit__ 吞下所有错误,然后在文档中写下“如果你不调用 persistChanges 你可能会丢失你的更改在错误情况下”?

我的具体用例是:我正在向其他开发人员提供 Python API,我想知道如何处理这种“关闭资源时出错”的情况,以便我的 API 遵循 Python 最佳实践/满足 Python 开发人员的期望使用我的图书馆。

解决方法

我建议为您的库创建自定义错误/警告类。这可以非常非常简单。已经存在一组可以扩展的 built-in exceptions。根据您上面的描述,我建议像这样扩展 RuntimeError

class MyLibraryConnectionError(RuntimeError):
    pass

或者,如果您只想发出警告,请像这样使用 ResourceWarning

class MyLibraryConnectionWarning(ResourceWarning):
    pass

还有 RuntimeWarning 可以扩展为类似的效果。

如果您觉得 ResourceWarningRuntimeWarningRuntimeError 没有准确描述异常,您也可以让它们直接从 Exception 或 {{ 1}},具体取决于您是希望它们仅在开发者模式下被标记(警告),还是希望获得完整的异常功能。

您可以像抛出任何其他异常一样抛出这些:

Warning

甚至捕获您的依赖项抛出的异常:

throw MyLibraryConnectionError("The underlying resource failed to close")
throw MyLibraryConnectionWarning("The underlying resource failed to close")

然后您的用户可以像这样实现:

def __exit__(...):
    try:
        # potentially dangerous code with connections
        underlyingConnection.close()
    except TheErrorYouSeeThrown as e: # you can probably make this Exception instead of TheErrorYouSeeThrown. The more specific the better,so you don't accidentally catch random errors you didn't mean to.
        throw MyLibraryConnectionError(e.message) # or whatever this needs to be,depending on the thrown exception type

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...