Skip to content

Commit

Permalink
refactor: combine 2 try-catches
Browse files Browse the repository at this point in the history
  • Loading branch information
Topology2333 committed Nov 10, 2023
1 parent 858c082 commit 851d673
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions hinghwa-dict-backend/article/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,20 @@ def get(self, request, id) -> JsonResponse: # 注意没有使用request,位

try:
comment = Comment.objects.get(id=id)
# 这里为什么不是 except Comment.DoesNotExist
except:
raise CommentNotFoundException(id)

# 登录获取 user 信息
try:
# 登录获取 user 信息
token = token_pass(request.headers)
user = token_user(token)
except UnauthorizedException:
return JsonResponse({"comment": comment_all(comment), "me": me}, status=401)

is_liked = comment.like_users.filter(id=user.id).exists()
is_author = user == comment.user if user else False
is_liked = comment.like_users.filter(id=user.id).exists()
is_author = user == comment.user if user else False

me = {"is_liked": is_liked, "is_author": is_author}
me = {"is_liked": is_liked, "is_author": is_author}

except Comment.DoesNotExist:
raise CommentNotFoundException(id)
except UnauthorizedException:
return JsonResponse({"comment": comment_all(comment), "me": me}, status=401)

return JsonResponse({"comment": comment_all(comment), "me": me}, status=200)

Expand Down

0 comments on commit 851d673

Please sign in to comment.