0

我正在使用flask flask-restx. 我希望我的所有回复都像这样格式化

{
"code": 200,
"status": "success",
"message": "OK",
"data": {
         
     }
}
from flask import request
from flask_restx import Resource
from ..service.quiz_services import QuizService
from ..utils.dto import QuizDto

api = QuizDto.api


@api.route("/")
class QuizController(Resource):
    def post(self):
        data = request.json
        return QuizService.create_quiz(data)

我什么时候GET在以下 URL 上发出请求,它返回

{
    "message": "The method is not allowed for the requested URL."
}

这是预期的,因为我在类中没有get方法QuizController,如何自定义此错误消息

4

1 回答 1

0

您可以在应用程序级别捕获特定异常

@app.errorhandler(Exception)
def catch_error(err):
    return "hello", 400

如果您只想捕获 404 异常,可以使用以下命令:

from werkzeug.exceptions import NotFound
@app.errorhandler(NotFound)
def catch_error(err):
    return "hello", 400
于 2021-11-09T13:50:42.727 回答