作为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个问题,我得到的不是未经授权的错误,而是一个InternalServerError
if not authorized:
语句工作正常(如果我在其中放入打印,我可以看到它工作)。但是,由于某些原因,我在尝试提出错误时得到以下信息:
During handling of the above exception, another exception occurred:
我遇到了this PEP article,它似乎建议改为raise UnauthorizedError from None
,但问题仍然存在。有人知道我如何才能成功地实现这一点吗?理想情况下,我希望在场景2和场景3中出现相同的错误,否则可能会有人从返回的错误中知道数据库中是否存在电子邮件
if语句正在引发UnAuthorized,但这种情况发生在excepts中,您必须引发DoesNotExist才能使其在except中引发UnAuthorized
相关问题 更多 >
编程相关推荐