Python |通过自定义异常返回400

2024-09-30 08:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一个自定义异常处理类来验证输入请求数据,并在验证失败时引发它。这是一个烧瓶应用程序。直到时间服务器还没有发送响应之前,这似乎一直在工作,因为我可以看到异常对象有一个自定义错误,但随后它被转换为一个通用的500错误消息。我是不是错过了什么?你做错了什么?你知道吗

class CustException(Exception):
    pass

class APIException(CustException):
    def __init__(self, status=None, title=None, type=None, detail=None, **kwargs):
        super(AIVException, self).__init__(detail)
        self.status = status
        self.detail = detail
        self.title = title
        self.type = type

if x not in [1, 2]:
    raise APIException(status=400, title='Bad request', type=default_type, detail="pass the correct value")

**

Note - I want this to do by raising an exception instead of using error handler. So please don't mark it duplicate.

**


Tags: 数据selfnone应用程序烧瓶titleinittype
1条回答
网友
1楼 · 发布于 2024-09-30 08:29:27

如果要使用APIException返回自定义消息或其他内容,可以使用errorhandler装饰器

@app.errorhandler(APIException)
def api_exception(ex):
    return 'Your custom message', your_code

相关问题 更多 >

    热门问题