捕获Python Flask RestPlus上的异常

时间:2018-01-08 21:55:49

标签: python flask flask-restful flask-restplus

当我对其中一个API的调用没有返回任何结果时,我正在努力寻找一种方法来返回响应。我正在使用flask_restplus,并尝试使用@ api.errorhandler注释但没有成功,然后我尝试在我的类中尝试一个简单的try / except块。

在这个特定问题中,我试图调用端点,其中GUID是数据库中不存在的用户。

http://localhost:5000/api/users/b3d8e86b-f2ad-4b6a-b768-e7adc1d94ced

用户终点定义如下:

import logging

from flask import Response
from flask_restplus import Resource, fields
from neomodel.core import DoesNotExist

from app.api.restplus import api
from app.models import User

log = logging.getLogger(__name__)

ns = api.namespace('users', description='Users')

user = api.model('user', {
    'id': fields.Integer(readOnly=True, description='The internal id of of a user'),
    'user_id': fields.String(required=True, description='The user unique identifier')}
)

@ns.route('/')
@api.response(404, 'Users not found.')
class UsersList(Resource):

    @api.marshal_list_with(user)
    def get(self):
        """
        Returns a list of all users
        :return: list_of_users
        """
        try:
            users = User.nodes
            list_of_users = list(users)
            return list_of_users
    #        return json.dumps(dict(users = [user.serialize for user in list_of_users]))
        except DoesNotExist:
            return Response(('{"No Content": "No user nodes found"}'), status = 200, mimetype = 'application/json')


@ns.route('/<string:user_id>')
class UserItem(Resource):

   @api.marshal_with(user)
   def get(self, user_id):
        """
        Returns a user with the given user_id
        :param id: user_id
        :return: user
        """
        try:
            user = User.nodes.get(user_id=user_id)
            return user
        except User.DoesNotExist:
            return Response({'message': 'Could not locate a user with the user_id provided.'}, 404)

我的初始化是在app / init .py:

中完成的
import logging.config
from flask import Flask, Blueprint
from neomodel import clear_neo4j_database
from neomodel import db

from app.config import Configuration
from app.web_app.routes import webapp_mod
from app.data_loader.routes import dataloader_mod
from app.utilities import prepare_rerun
from app.api.endpoints.users import ns as users_namespace
from app.api.endpoints.sessions import ns as sessions_namespace
from app.api.endpoints.browsers import ns as browsers_namespace
from app.api.endpoints.os import ns as os_namespace
from app.api.endpoints.applications import ns as applications_namespace
from app.api.endpoints.tenants import ns as tenants_namepspace
from app.api.endpoints.devices import  ns as devices_namespace
from app.api.endpoints.environments import ns as environments_namespace

from app.api.restplus import api
from os import path


app = Flask(__name__)
app.config.from_object(Configuration)
app.register_blueprint(dataloader_mod, url_prefix='/data_loader')
log_file_path = path.join(path.dirname(path.abspath(__file__)), 'logging.conf')
logging.config.fileConfig(log_file_path)
log = logging.getLogger(__name__)

blueprint = Blueprint('api', __name__, url_prefix='/api')
api.init_app(blueprint)
api.add_namespace(users_namespace)
api.add_namespace(sessions_namespace)
api.add_namespace(browsers_namespace)
api.add_namespace(applications_namespace)
api.add_namespace(tenants_namepspace)
api.add_namespace(devices_namespace)
api.add_namespace(os_namespace)
api.add_namespace(environments_namespace)

我在此处定义api的resplus.py模块只有api对象的定义,但我曾尝试在线跟踪一些示例并在其中定义通过users对象中的注释处理异常的方法

from flask_restplus import Api
from neomodel.core import DoesNotExist

api = Api(version='1.0', title='Users Activity Log',
          description='An API to retreive information about users'' activities in Infor Ming.le')

然而,当我拨打电话时,我得到的是:#/ p>而不是获得带有消息和404代码的JSON响应。

{
    "id": null,
    "user_id": null
}

提前感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

这可能不再有意义,因为您的问题是很久以前的,但是我在寻找flask-restplus异常处理最佳实践时遇到了这个问题。我认为您的问题不在于flask-restplus的异常处理。听起来好像当用户不存在时,您的User类没有引发DidNotExist异常。您是否已验证自己是否正在击打Except:障碍物?

无论如何,一旦您确认确实引发了DidNotExist异常,就可以在 init.py 中为该异常注册一个错误处理程序,如下所示:

@api.errorhandler(DoesNotExist)
def handle_user_does_not_exist_error(error):
    return ({'message': 'Could not locate a user with the user_id provided.'}, 404) 

答案 1 :(得分:0)

我想我为您提供帮助的时间有点晚了,但我可能会帮助其他遇到相同问题的人

问题是,尽管您还有另一个返回码(并且有错误),烧瓶仍然尝试使用@marshal_with(user)。一开始可能会有些混乱。

如果您查看此answer,则可以看到如何返回多种答案类型而又不会失去敏捷的功能,而且您还会得到编组信息。比标准@marshal_with更具通用性。

还有其他方法可以取得相似的错误结果:您可以使用abort()函数(在某些情况下可能更合逻辑)。

因此换句话说,转换后的代码看起来像这样:

import logging

from flask import Response
from flask_restplus import Resource, fields, marshal
from neomodel.core import DoesNotExist

from app.api.restplus import api
from app.models import User

log = logging.getLogger(__name__)

ns = api.namespace('users', description='Users')

user = api.model('user', {
    'id': fields.Integer(readOnly=True, description='The internal id of of a user'),
    'user_id': fields.String(required=True, description='The user unique identifier')}
)

@ns.route('/')
@api.response(404, 'Users not found.')
class UsersList(Resource):

    @api.response(model=user, code=200)
    @api.response(404, 'Users not found.')
    def get(self):
        """
        Returns a list of all users
        :return: list_of_users
        """
        try:
            users = User.nodes
            list_of_users = list(users)
            return marshal(list_of_users)
        except DoesNotExist:
            return {"No Content": "No user nodes found"}, 404


@ns.route('/<string:user_id>')
class UserItem(Resource):

   @api.response(model=user, code=200)
   @api.response(code=404, 'Users not found.')
   def get(self, user_id):
        """
        Returns a user with the given user_id
        :param id: user_id
        :return: user
        """
        try:
            user = User.nodes.get(user_id=user_id)
            return marshal(user)
        except User.DoesNotExist:
            return {'message': 'Could not locate a user with the user_id provided.'}, 404

# More Endpoints - I hope this did the job...

答案 2 :(得分:0)

你可以使用

from flask_restplus import abort
abort(400, custom='value')

中止和通过错误

namespace.abort(400, "An error occured")