4-9自定义异常报错,老师帮忙看看呢
来源:4-9 自定义APIException

aumujun
2021-06-07
报错如下
127.0.0.1 - - [07/Jun/2021 22:29:34] "POST /v1/client/register HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1966, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/wrappers/response.py", line 263, in force_type
response = Response(*run_wsgi_app(response, environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/test.py", line 1229, in run_wsgi_app
app_rv = app(environ, start_response)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 221, in __call__
response = t.cast("WSGIResponse", self.get_response(environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 209, in get_response
headers = self.get_headers(environ, scope)
TypeError: get_headers() takes from 1 to 2 positional arguments but 3 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 2301, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 2287, in wsgi_app
response = self.handle_exception(e)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1733, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 2284, in wsgi_app
response = self.full_dispatch_request()
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1808, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1823, in finalize_request
response = self.make_response(rv)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1974, in make_response
reraise(TypeError, new_error, sys.exc_info()[2])
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1966, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/wrappers/response.py", line 263, in force_type
response = Response(*run_wsgi_app(response, environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/test.py", line 1229, in run_wsgi_app
app_rv = app(environ, start_response)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 221, in __call__
response = t.cast("WSGIResponse", self.get_response(environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 209, in get_response
headers = self.get_headers(environ, scope)
TypeError: get_headers() takes from 1 to 2 positional arguments but 3 were given
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a ClientTypeError.
c^C%
(restful) mac@macdeMac-mini ~/Documents/restful
(restful) ✘ mac@macdeMac-mini ~/Documents/restful
(restful) ✘ mac@macdeMac-mini ~/Documents/restful python3 ginger.py
* Serving Flask app "app.app" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 917-437-446
127.0.0.1 - - [07/Jun/2021 22:33:29] "POST /v1/client/register HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1966, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/wrappers/response.py", line 263, in force_type
response = Response(*run_wsgi_app(response, environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/test.py", line 1229, in run_wsgi_app
app_rv = app(environ, start_response)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 221, in __call__
response = t.cast("WSGIResponse", self.get_response(environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 209, in get_response
headers = self.get_headers(environ, scope)
TypeError: get_headers() takes from 1 to 2 positional arguments but 3 were given
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 2301, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 2287, in wsgi_app
response = self.handle_exception(e)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1733, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 2284, in wsgi_app
response = self.full_dispatch_request()
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1808, in full_dispatch_request
return self.finalize_request(rv)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1823, in finalize_request
response = self.make_response(rv)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1974, in make_response
reraise(TypeError, new_error, sys.exc_info()[2])
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/_compat.py", line 34, in reraise
raise value.with_traceback(tb)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/flask/app.py", line 1966, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/wrappers/response.py", line 263, in force_type
response = Response(*run_wsgi_app(response, environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/test.py", line 1229, in run_wsgi_app
app_rv = app(environ, start_response)
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 221, in __call__
response = t.cast("WSGIResponse", self.get_response(environ))
File "/Users/mac/.local/share/virtualenvs/restful-V3yZnvwa/lib/python3.7/site-packages/werkzeug/exceptions.py", line 209, in get_response
headers = self.get_headers(environ, scope)
TypeError: get_headers() takes from 1 to 2 positional arguments but 3 were given
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a ClientTypeError.
代码如下,老师帮忙看看呢
from flask import request, json
from werkzeug.exceptions import HTTPException
class APIException(HTTPException):
code = 500
msg = 'sorry, we made a mistake (* ̄︶ ̄)!'
error_code = 999
def __init__(self, msg=None, code=None, error_code=None,
headers=None):
if code:
self.code = code
if error_code:
self.error_code = error_code
if msg:
self.msg = msg
super(APIException, self).__init__(msg, None)
def get_body(self, environ=None):
body = dict(
msg=self.msg,
error_code=self.error_code,
request=request.method + ' ' + self.get_url_no_param()
)
text = json.dumps(body)
return text
def get_headers(self, environ=None):
"""Get a list of headers."""
return [('Content-Type', 'application/json')]
@staticmethod
def get_url_no_param():
full_path = str(request.full_path)
main_path = full_path.split('?')
return main_path[0]
3回答
-
三棵
2021-09-11
我也遇到同样问题,开始也是一脸懵逼. 看到网探同学说的Werkzeug版本问题, 于是试了下卸载当前版本werkzeug重新安装werkzeug 1.0, 可以正常返回结果.
遇到同样问题的同学,在werkzeug2.0版本下解决也很简单, 因为2.0版本的`get_body()` 和`get_headers()`中多了一个参数,导致我们继承自定义`HTTPException`类的子类无法继承改写这两个方法,于是返回的自定义类不被认为是一个有效的response. 只要进入源码把`get_body()` 和`get_headers()`这两个类的参数也复制下来写到我们的自定义类里就可以了.
def get_body(
self,
environ: t.Optional["WSGIEnvironment"] = None,
scope: t.Optional[dict] = None,
) -> str:
body = dict(
msg=self.msg,
error_code=self.error_code,
request=request.method + ' ' + self.get_url_no_param()
)
text = json.dumps(body)
return text
def get_headers(
self,
environ: t.Optional["WSGIEnvironment"] = None,
scope: t.Optional[dict] = None,
) -> t.List[t.Tuple[str, str]]:
"""Get a list of headers."""
return [('Content-Type', 'application/json')]
00 -
网探
2021-07-22
这个问题主要是现在安装的Flask自带安装了Werkzeug2.0.1引起,之前老师的代码Werkzeug是1.0.1的,我之前能正常的代码,现在在新达的环境也是无法运行了,自定的APIException报同样错误。希望老师可以有兼容Werkzeug2的代码出来。
00 -
7七月
2021-06-09
看这个提示猜不出问题。编程需要自己调试下,或者再看下视频、核对下源码。
00
相似问题