在Python中报告关闭资源时发生的错误的最佳实践是什么?
特别是,如果我实现__enter__
和__exit__
,我可以使用
with my_library.connect(endpoint) as connection:
connection.store_data("...")
但是,如果关闭连接并持久化更改失败,例如由于网络中断,我该怎么办?我知道我可以在技术上从__exit__
中引发错误,但这是Python中的最佳实践/习惯做法吗?或者我是否应该,例如,提供一个单独的persistChanges
方法,让__exit__
吞下所有错误,然后在文档中写道"如果不调用persistChanges
,可能会丢失错误情况下的更改"?
我的具体用例是:我正在向其他开发人员提供Python API,我想知道如何处理"关闭资源时出错"的情况。这样我的API遵循Python最佳实践/满足使用我的库的Python开发人员的期望。
我建议为您的库创建一个自定义的错误/警告类。这可以非常非常简单。已经存在一组内置异常,您可以从中进行扩展。根据你上面的描述,我建议像这样扩展RuntimeError
:
class MyLibraryConnectionError(RuntimeError):
pass
或者,如果您只想抛出一个警告,使用ResourceWarning
,像这样:
class MyLibraryConnectionWarning(ResourceWarning):
pass
还可以扩展RuntimeWarning
以达到类似的效果。
如果你觉得ResourceWarning
,RuntimeWarning
和RuntimeError
没有准确地描述异常,你也可以让它们直接从Exception
或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
那么你的用户可以这样实现:
try:
with my_library.connect(endpoint) as connection:
connection.store_data("...")
except my_library.MyLibraryConnectionError as e:
# handle appropriately, or ignore
except Exception as e:
# Handle other errors that happen, that your library doesn't cause.