-
Notifications
You must be signed in to change notification settings - Fork 974
Open
Description
在6-three-rituals-of-exceptions-handling.md中
3. 异常处理不应该喧宾夺主
有以下异常处理代码
def upload_avatar(request):
"""用户上传新头像"""
try:
avatar_file = request.FILES['avatar']
except KeyError:
raise error_codes.AVATAR_FILE_NOT_PROVIDED
try:
resized_avatar_file = resize_avatar(avatar_file)
except FileTooLargeError as e:
raise error_codes.AVATAR_FILE_TOO_LARGE
except ResizeAvatarError as e:
raise error_codes.AVATAR_FILE_INVALID
try:
request.user.avatar = resized_avatar_file
request.user.save()
except Exception:
raise error_codes.INTERNAL_SERVER_ERROR
return HttpResponse({})
建议利用上下文管理器来改善我们的异常处理流程
class raise_api_error:
"""captures specified exception and raise ApiErrorCode instead
:raises: AttributeError if code_name is not valid
"""
def __init__(self, captures, code_name):
self.captures = captures
self.code = getattr(error_codes, code_name)
def __enter__(self):
# 该方法将在进入上下文时调用
return self
def __exit__(self, exc_type, exc_val, exc_tb):
# 该方法将在退出上下文时调用
# exc_type, exc_val, exc_tb 分别表示该上下文内抛出的
# 异常类型、异常值、错误栈
if exc_type is None:
return False
if exc_type == self.captures:
raise self.code from exc_val
return False
为什么突然增加了raise self.code from exc_val 的写法
而上文中没有写成
try:
avatar_file = request.FILES['avatar']
except KeyError as e:
raise error_codes.AVATAR_FILE_NOT_PROVIDED from e
longqzh and AtomRun
Metadata
Metadata
Assignees
Labels
No labels