python - 如何在 Flask-reSTLess 中返回 "already exists"错误?

标签 python validation exception-handling flask-sqlalchemy flask-restless

我想做一些异常处理程序。我在 python 中结合使用了 Flask-reSTLess 和 SQLAlchemy。

我的问题:

当我使用数据库中已存在的对象向 api 发送请求时,SQLAlchemy 显示异常:

IntegrityError: (IntegrityError) column <column_name> is not unique u'INSERT INTO ...

所以我尝试将属性 validation_exceptions 添加到 create_api 方法中:

manager.create_api( ... , validation_exceptions=[IntegrityError])

但响应 json 包含:

{
    "validation_errors": "Could not determine specific validation errors"
} 

并且服务器 api 显示异常:

Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask_restless\views.py", line 797, in _extract_error_messages
    left, right = str(exception).rsplit(':', 1)
ValueError: need more than 1 value to unpack

Flask-reSTLess 中的异常验证不适用于此类异常(IntegrityError)

我该怎么办?是否可以为异常创建一些处理程序并在 json 中返回我自己的错误消息?

最佳答案

documentation (v0.17.0 到此发布日期)状态:

Currently, Flask-Restless expects that an instance of a specified validation error will have a errors attribute, which is a dictionary mapping field name to error description (note: one error per field).

因此,要更改 validation_errors 的内容,您的异常需要一个包含字典的 errors 属性。此字典的内容将作为 validation_errors 出现在服务器响应中。

来自 flask-restless/tests/test_validation.py :

class TestSimpleValidation(ManagerTestBase):
"""Tests for validation errors raised by the SQLAlchemy's simple built-in
validation.
For more information about this functionality, see the documentation for
:func:`sqlalchemy.orm.validates`.
"""

def setup(self):
    """Create APIs for the validated models."""
    super(TestSimpleValidation, self).setup()

    class Person(self.Base):
        __tablename__ = 'person'
        id = Column(Integer, primary_key=True)
        age = Column(Integer, nullable=False)

        @validates('age')
        def validate_age(self, key, number):
            if not 0 <= number <= 150:
                exception = CoolValidationError()
                exception.errors = dict(age='Must be between 0 and 150')
                raise exception
            return number

        @validates('articles')
        def validate_articles(self, key, article):
            if article.title is not None and len(article.title) == 0:
                exception = CoolValidationError()
                exception.errors = {'articles': 'empty title not allowed'}
                raise exception
            return article

    class Article(self.Base):
        __tablename__ = 'article'
        id = Column(Integer, primary_key=True)
        title = Column(Unicode)
        author_id = Column(Integer, ForeignKey('person.id'))
        author = relationship('Person', backref=backref('articles'))

    self.Article = Article
    self.Person = Person
    self.Base.metadata.create_all()
    self.manager.create_api(Article)
    self.manager.create_api(Person, methods=['POST', 'PATCH'],
                            validation_exceptions=[CoolValidationError])

要求:

data = dict(data=dict(type='person', age=-1))
response = self.app.post('/api/person', data=dumps(data))

响应:

HTTP/1.1 400 Bad Request

{ "validation_errors":
    {
      "age": "Must be between 0 and 150",
    }
}

关于python - 如何在 Flask-reSTLess 中返回 "already exists"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547712/

相关文章:

python - boost::python:将 C++ 类暴露给嵌入在 C++ 应用程序中的 python 脚本

python - 在CKAN中上传文件但收到 'Could not connect to DataPusher'错误

java - 在 JSF 1.2 自定义组件中指定 Validator

Python:检测物理非 HT CPU 的跨平台解决方案?

python - 如何使 python urllib2 遵循重定向并保留 post 方法

ruby-on-rails - 在 Active Record 回调中验证日语字符

java - 通配符域 validator

delphi - 是否可以从引发异常的点恢复执行?

来自函数的 c++ stacktrace 抛出异常?

c++ - 为什么构造函数不能优雅地处理错误?