抛出异常的最佳实践
以前我都是用方法一进行抛出异常,但是方法一看不到详细的堆栈,很不爽,所以还是用方法二比较爽
# 方法一
LOG.error("cwd '{}' error, ex: {}".format(dir_name, ex))
# 方法二
LOG.error("stor '{}' error.".format(file_to_upload), exc_info=1)
方法一
2021-12-07 16:47:47,648.648 17898:140560392476480 ERROR __main__:226 stor 'retr_test.file' error. ex: a bytes-like object is required, not 'str'
方法二
2021-12-07 16:43:07,779.779 16164:139954958853952 ERROR __main__:226 stor 'retr_test.file' error.
Traceback (most recent call last):
File "ftp_client.py", line 218, in __create_remote_file
'stor {}'.format(file_to_upload), open(file_to_upload, 'r'))
File "/usr/lib64/python3.6/ftplib.py", line 538, in storlines
if buf[-1] in B_CRLF: buf = buf[:-1]
TypeError: a bytes-like object is required, not 'str'