我的Flask路线定义如下:
@app.route('/api/v0.1/getTopPosts', methods = ['POST'])
@auth.login_required
def get_top_posts():
if not request.json or not 'page' in request.json:
abort(400)
resultsPerQuery = 10
page = int(request.json['page'])
skip = ( page - 1) * resultsPerQuery;
postsToReturn = []
errorDidOccur = False
#get top posts
try:
topPostsReturn = db.collection.find({"isActive":"yes","state":{ "$BAD" : False }}).sort('rank',1).limit(resultsPerQuery).skip(skip)
for topPost in topPostsReturn:
postsToReturn.append(db.collection.find_one({'_id':topPost['posts_id']}).copy())
except pymongo.errors.OperationFailure as e:
errorDidOccur = True
if errorDidOccur:
reply = {
'reply' : 'failure',
'error' : e,
'data' : None
}
else:
reply = {
'reply' : 'success',
'error' : None,
'data' : postsToReturn
}
return dumps(reply)
我插入$ BAD以生成OperationalFailure异常。我遇到的问题是' e'不是JSON可序列化的。我明白了:
TypeError: OperationFailure(u'database error: invalid operator: $BAD',) is not JSON serializable
如何报告错误?
由于
答案 0 :(得分:2)
使用
'error' : e.message,
而不是
'error' : e,