Fabric:处理异常,以便我可以在 try 块中捕获它
Fabric: handle exception so I can catch it in a try block
我正在尝试找到一种方法来处理 Fabric 错误。
示例:如果我的远程主机因维护而停机,而我的应用程序是 运行,试图在其上完成一些工作,我从 Fabric 收到异常。这会阻止我的应用程序,并打印出可怕的堆栈跟踪。
为了避免这种情况,因为我需要知道主机何时关闭,我将我的请求包装到远程,使用 try 块,但我不确定我应该在 except 块中放什么,只捕获来自 Fabric 的异常(如 networkError 和类似的)。
使用 Fabric 处理 try-except 块的正确方法是什么?或者更具体地说,我在 "except" 块中放了什么,这样它就会捕获来自 fabric 的任何异常,并避免让整个应用程序退出并打印出堆栈错误?
所以您想知道这种情况何时以某种方式发生?如何捕捉这个异常(具有可能导致这种情况的确切异常集,不要试图捕捉太多)并让它通过电子邮件发送给您或类似的东西并重新引发相同的异常以便您知道发生了什么。
编辑。很容易在屏幕上不显示回溯,但我不确定我是否建议完全避免回溯。实现日志记录是一个很好的选择,或者将回溯格式化为您想要的内容也是可以接受的。查看回溯模块https://docs.python.org/2/library/traceback.html
logging.exception
将在捕获错误后输出回溯:
import logging
try:
1/0
except Exception as e:
logging.exception(e)
ERROR:root:division by zero
Traceback (most recent call last):
File "/home/padraic/Dropbox/python/py3/size.py", line 105, in <module>
1/0
ZeroDivisionError: division by zero
Process finished with exit code 0
如果您查看文档中的 failure-handling,您会发现您可以使用上下文管理器仅在您获得非零退出状态时发出警告。
Once the task list has been constructed, Fabric will start executing them as outlined in Execution strategy, until all tasks have been run on the entirety of their host lists. However, Fabric defaults to a “fail-fast” behavior pattern: if anything goes wrong, such as a remote program returning a nonzero return value or your fabfile’s Python code encountering an exception, execution will halt immediately.
This is typically the desired behavior, but there are many exceptions to the rule, so Fabric provides env.warn_only, a Boolean setting. It defaults to False, meaning an error condition will result in the program aborting immediately. However, if env.warn_only is set to True at the time of failure – with, say, the settings context manager – Fabric will emit a warning message but continue executing.
from fabric.api import settings
with settings(warn_only=True):
p = run('sudo foo')
if p.return_code == 0:
...
elif p.return_code == 1:
....
还有一个env.abort_exception:
默认值:None
Fabric normally handles aborting by printing an error message to stderr and calling sys.exit(1). This setting allows you to override that behavior (which is what happens when env.abort_exception is None.)
Give it a callable which takes a string (the error message that would have been printed) and returns an exception instance. That exception object is then raised instead of SystemExit (which is what sys.exit does.)
Much of the time you’ll want to simply set this to an exception class, as those fit the above description perfectly (callable, take a string, return an exception instance.) E.g. env.abort_exception = MyExceptionClass.
我正在尝试找到一种方法来处理 Fabric 错误。
示例:如果我的远程主机因维护而停机,而我的应用程序是 运行,试图在其上完成一些工作,我从 Fabric 收到异常。这会阻止我的应用程序,并打印出可怕的堆栈跟踪。
为了避免这种情况,因为我需要知道主机何时关闭,我将我的请求包装到远程,使用 try 块,但我不确定我应该在 except 块中放什么,只捕获来自 Fabric 的异常(如 networkError 和类似的)。
使用 Fabric 处理 try-except 块的正确方法是什么?或者更具体地说,我在 "except" 块中放了什么,这样它就会捕获来自 fabric 的任何异常,并避免让整个应用程序退出并打印出堆栈错误?
所以您想知道这种情况何时以某种方式发生?如何捕捉这个异常(具有可能导致这种情况的确切异常集,不要试图捕捉太多)并让它通过电子邮件发送给您或类似的东西并重新引发相同的异常以便您知道发生了什么。
编辑。很容易在屏幕上不显示回溯,但我不确定我是否建议完全避免回溯。实现日志记录是一个很好的选择,或者将回溯格式化为您想要的内容也是可以接受的。查看回溯模块https://docs.python.org/2/library/traceback.html
logging.exception
将在捕获错误后输出回溯:
import logging
try:
1/0
except Exception as e:
logging.exception(e)
ERROR:root:division by zero
Traceback (most recent call last):
File "/home/padraic/Dropbox/python/py3/size.py", line 105, in <module>
1/0
ZeroDivisionError: division by zero
Process finished with exit code 0
如果您查看文档中的 failure-handling,您会发现您可以使用上下文管理器仅在您获得非零退出状态时发出警告。
Once the task list has been constructed, Fabric will start executing them as outlined in Execution strategy, until all tasks have been run on the entirety of their host lists. However, Fabric defaults to a “fail-fast” behavior pattern: if anything goes wrong, such as a remote program returning a nonzero return value or your fabfile’s Python code encountering an exception, execution will halt immediately.
This is typically the desired behavior, but there are many exceptions to the rule, so Fabric provides env.warn_only, a Boolean setting. It defaults to False, meaning an error condition will result in the program aborting immediately. However, if env.warn_only is set to True at the time of failure – with, say, the settings context manager – Fabric will emit a warning message but continue executing.
from fabric.api import settings
with settings(warn_only=True):
p = run('sudo foo')
if p.return_code == 0:
...
elif p.return_code == 1:
....
还有一个env.abort_exception:
默认值:None
Fabric normally handles aborting by printing an error message to stderr and calling sys.exit(1). This setting allows you to override that behavior (which is what happens when env.abort_exception is None.)
Give it a callable which takes a string (the error message that would have been printed) and returns an exception instance. That exception object is then raised instead of SystemExit (which is what sys.exit does.)
Much of the time you’ll want to simply set this to an exception class, as those fit the above description perfectly (callable, take a string, return an exception instance.) E.g. env.abort_exception = MyExceptionClass.