Python |通过自定义异常返回400
我编写了一个自定义异常处理类来验证输入请求数据,并在验证失败时引发它。这是一个烧瓶应用程序。这似乎一直在工作,直到时间服务器没有发送响应,因为我可以看到异常对象有一个自定义错误,但随后它被转换为一个通用的500错误消息。我是不是错过了什么?你做错了什么Python |通过自定义异常返回400,python,flask,Python,Flask,我编写了一个自定义异常处理类来验证输入请求数据,并在验证失败时引发它。这是一个烧瓶应用程序。这似乎一直在工作,直到时间服务器没有发送响应,因为我可以看到异常对象有一个自定义错误,但随后它被转换为一个通用的500错误消息。我是不是错过了什么?你做错了什么 class CustException(Exception): pass class APIException(CustException): def __init__(self, status=None, title=None
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")
**
注意-我希望通过引发异常而不是使用
错误处理程序。所以请不要将其标记为重复
**如果要使用APIException返回自定义消息或其他内容,可以使用decorator
@app.errorhandler(APIException)
def api_exception(ex):
return 'Your custom message', your_code
当超类没有构造函数时,为什么要使用
。\uuuuu init\uuuu(detail)
。但这并不影响反应。不是吗?所以如果语法中有错误,那么您将看到一个500错误。尝试使用。\uuuuu init\uuuuu()
而不是。\uuuuu init\uuuuuu(细节)
,然后查看您是否仍然可以500@AbtPst-删除超级调用后不工作。您可以保留超级调用,但构造函数应为空