前段时间我写了一段代码,一条Flask路由从我正在处理的Web应用程序中注销用户,看起来像这样:
@app.route('/logout')
@login_required
def logout():
# lets get the user cookie, and if it exists, delete it
cookie = request.cookies.get('app_login')
response = make_response(redirect(url_for('login')))
if cookie:
riak_bucket = riak_connect('sessions')
riak_bucket.get(cookie).delete()
response.delete_cookie('app_login', None)
return response
return response
我完成了它的工作,并且确实在工作,但现在我正在通过添加适当的错误处理来使应用程序更加健壮,这是我之前在我的代码中无法大规模完成的事情。所以我偶然发现了这个路线功能,我开始写它的新版本,当我意识到我不知道怎么做'正确的方式'。以下是我提出的建议:
@app.route('/logout')
@login_required
def logout():
# why dont we call variables after what they are in specifics?
login_redirect = make_response(redirect(url_for('login')))
try:
cookie = request.cookies.get('app_login')
except:
return login_redirect
# if we are here, the above try/except went good, right?
try:
# perhaps sessions_bucket should really be bucket_object?
# is it valid to chain try statements like that, or should they be
# tried separately one by one?
sessions_bucket = riak_connect('sessions')
sessions_bucket.get(cookie).delete()
login_redirect.delete_cookie('app_login', None)
except:
return login_redirect
# return redirect by default, just because it seems more secure
return login_redirect
它也做到了,但仍然看起来不对我。所以,对于所有在编写真正pythonic Python代码方面具有较大经验的人来说,问题是,我希望我能够很好地处理所有错误的代码,让其他人可读并快速完成其工作(特别是案例,但也在相当大的代码库的其余部分):
提前致谢!
答案 0 :(得分:-1)
我不知道确切的riak python API,所以我不知道抛出了什么异常。另一方面,Web应用程序应该如何在不同的错误条件下运行?是否要通知用户?
一些建议:
import logging
log = loggin.getLogger(__name__)
@app.route('/logout')
@login_required
def logout():
login_redirect = make_response(redirect(url_for('login')))
try:
sessionid = request.cookies.get('app_login', None)
except AttributeError:
sessionid = None
log.error("Improperly configured")
if sessionid:
try:
session_store = riak_connect('sessions')
session = session_store.get(sessionid)
if session:
session.delete()
login_redirect.delete_cookie('app_login', None)
except IOError: # what errors appear when connect fails?
log.exception("during logout")
return login_redirect