[英]What's the catch-all Exception class regarging file storage handling in Django?
我需要捕获与Django的File Storage API相关的所有异常,可能是用于读取或写入等。这里的问题是该API中没有定义任何通用异常类。 例如,当使用FileSystemStorage
(默认设置)时,抛出的异常是IOError
,但是如果我使用的是远程存储,例如S3? 我知道我可以只添加一些通用的Boto异常,但是我想要的是保持此代码通用,并与以后选择的任何存储后端分离。
这是解释这种情况的示例代码:
import contextlib
class SomeForm(forms.ModelForm):
textfield = forms.CharField()
class Meta:
model = CSSTemplate
def __init__(self, *args, **kwargs):
super(SomeForm, self).__init__(*args, **kwargs)
if not self.is_bound and self.instance and self.instance.file_field:
try:
with contextlib.closing(self.instance.file_field.file) as file_obj:
file_obj.open('r')
self['textfield'].field.initial = file_obj.read()
except (IOError, ): # <-- ???
self.instance.file_field = ''
我应该在except
子句中检查哪些异常类?
try:
except OSError:
//raise if file is already exist
except IOError:
// is not a directory
except ValueError:
//raise if someone is attempted to access then denied
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.