[英]Flask-restful - During handling of the above exception, another exception occurred
作為 Flask-restful API 的一部分,我有一個登錄資源:
class LoginApi(Resource):
def post(self):
try:
body = request.get_json()
user = User.objects.get(email=body.get('email'))
authorized = user.check_password(body.get('password'))
if not authorized:
raise UnauthorizedError
expires = datetime.timedelta(days=7)
access_token = create_access_token(identity=str(user.id), expires_delta=expires)
return {'token': access_token}, 200
except DoesNotExist:
raise UnauthorizedError
except Exception as e:
raise InternalServerError
登錄路由有4種場景:
所以對於數字 3 - 而不是得到 UnauthorizedError,我得到的是 InternalServerError。
if not authorized:
語句工作正常(如果我在那里放了一個打印,我可以看到它工作)。 但是由於某種原因,我在嘗試引發錯誤時收到以下信息:
在處理上述異常的過程中,又發生了一個異常:
我遇到了這篇 PEP 文章,它似乎建議更改為raise UnauthorizedError from None
但問題仍然存在。 有誰知道我如何成功實現這一點? 理想情況下,我希望從場景 2 和 3 中引發相同的錯誤,否則有人可能會從他們返回的錯誤中知道數據庫中是否存在電子郵件。
if 語句正在引發 UnAuthorized,但這種情況發生在異常中,您必須引發DoesNotExist 以使其能夠在異常中引發 UnAuthorized。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.