flask_restful中的自定义错误处理程序抛出NameError

时间:2017-09-14 06:24:23

标签: flask flask-restful

我正在尝试按照docs中给出的说明向flask_restful API添加自定义错误代码,但是我没有收到正确的响应,而是收到NameError: global name 'UnsupportedMediaType' is not defined消息。我在这里做错了什么?

# -*- coding: utf-8 -*-

from flask import Flask, request
from flask_restful import Resource, Api

import service

errors = {
    'UnsupportedMediaType': {
        'message': 'Unsupported Media Type',
        'status': 415
    }
}

app = Flask(__name__)
api = Api(app, errors=errors)

class Service(Resource):
    def post(self):
        if request.is_json:
            data = request.get_json()
            return service.handler(args['data'])
        else:
            raise UnsupportedMediaType

api.add_resource(Service, '/')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

1 个答案:

答案 0 :(得分:1)

您必须将错误处理程序存储到函数中。像这样:

System.setProperty("webdriver.gecko.driver","C:\\Utility\\BrowserDrivers\\geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.get("https://staging.keela.co/login"); WebDriverWait wait = new WebDriverWait (driver, 15); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='login-email']"))); element.sendKeys("bandanakeela@yopmail.com"); driver.findElement(By.xpath("//input[@id='login-password']")).sendKeys("keela"); driver.findElement(By.xpath("//button[@class='btn btn-sm btn-block btn-primary']")).click();

现在您可以根据需要调用错误处理程序。

您可以使用Flask错误处理程序装饰器。继承人link