我正在做一个Flask API项目,我在处理AssertionError异常时遇到问题。因此,我的程序检查指定参数的长度,如果长度超过限制,则引发AssertionError。问题是错误处理程序执行了两次。
__ init __。py
from flask_restplus import Api
from flask import Blueprint
from .main.controller.book_controller import api as book_ns
from .main.controller.category_controller import api as category_ns
from .main.exception.not_found import NotFound
blueprint = Blueprint('api', __name__)
api = Api(blueprint,
title='FLASK RESTPLUS API BOILER-PLATE WITH JWT',
version='1.0',
description='a boilerplate for flask restplus web service')
api.add_namespace(book_ns, path='/book')
api.add_namespace(category_ns, path='/category')
@api.errorhandler(NotFound)
def handle_no_result(error):
'''Return a custom not found error message and 404 status code'''
return {'message': error.message}, 404
@api.errorhandler(AssertionError)
def handle_assertion_error(error):
'''Return the assertion error message and 400 status code'''
print("&&&&&&&&&&&&&&&&&&")
return {'message': error.message}, 400
book.py(模型)
from .. import db
from sqlalchemy.schema import Sequence
from sqlalchemy.orm import validates
class Book(db.Model):
__tablename__ = 'book'
book_id = db.Column('book_id',
db.Integer,
Sequence('seq_book'),
primary_key=True)
viewable = db.Column('viewable', db.Integer)
title = db.Column('title', db.String(length=50), nullable=False)
isbn = db.Column('isbn', db.String(length=13), unique=True, nullable=False)
publication_date = db.Column('publication_date', db.Date)
presentation_month = db.Column('presentation_month', db.String(length=7))
image = db.Column('image', db.String(length=100))
category_id = db.Column('category_id', db.Integer,
db.ForeignKey('category.category_id'))
category = db.relationship('Category')
def __repr__(self):
return '<id {}>'.format(self.book_id)
def serialize(self):
return {
'book_id': int(self.book_id),
'isbn': self.isbn,
'title': self.title,
'category': self.category.serialize(),
'publication_date': self.publication_date,
'presentation_month': self.presentation_month,
'image': self.image
}
@validates('title')
def validates_title(self, key, title):
if not title:
raise AssertionError('title cannot be blank')
if len(title) > 50:
raise AssertionError(
'title length must be minor or equal to 50 characters')
@validates('isbn')
def validates_isbn(self, key, isbn):
if not isbn:
raise AssertionError('isbn cannot be blank')
if len(isbn) > 13:
raise AssertionError(
'isbn length must be minor or equal to 13 characters')
@validates('presentation_month')
def validates_presentation_month(self, key, presentation_month):
if len(presentation_month) > 7:
raise AssertionError(
'presentation_month length must be minor or equal to 7 characters'
)
@validates('image')
def validates_image(self, key, image):
if len(image) > 100:
raise AssertionError(
'image length must be minor or equal to 100 characters')
所以我在控制台中收到一个异常错误,它打印两次“ &&&&&&&&&&”,并发送状态代码500而不是de400。我认为它抛出500错误代码,因为它返回的是{{1 }}。我不知道如何解决这个问题。
--- EDIT -----------------------------:
控制台中返回的异常为:
return {'message': error.message}, 400