flask-RESTful:为什么在解析类型错误的参数时会出现AssertionError?

时间:2016-04-15 10:37:19

标签: python flask flask-restful

我第一次使用flask-RESTful。在docs中它说:

  

使用reqparse模块还可以免费获得合理的错误消息。如果参数未能通过验证,Flask-RESTful将响应400 Bad Request和突出显示错误的响应。

但是在我的情况下,我得到一个异常AssertionError。

以下是代码:

#! /usr/bin/python
# -*- coding: UTF-8 -*-

from flask import Flask
from flask.ext.restful import Api, Resource, reqparse


class Test(Resource):
    @staticmethod
    def put(id):
        parser = reqparse.RequestParser()
        parser.add_argument('arg1')
        parser.add_argument('arg2', type=int, help='helptext')
        args = parser.parse_args()
        return args, 200

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

api.add_resource(Test, '/v1.0/test/<int:id>', endpoint='test')

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

当我使用正确的值测试时,它可以工作:

$ curl -i  -H "Accept: application/json"  -X PUT --data "arg1=ABC&arg2=1"     http://192.0.0.7:5001/v1.0/test/1
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 38
Server: Werkzeug/0.8.3 Python/2.6.6
Date: Fri, 15 Apr 2016 11:59:48 GMT

{
    "arg1": "ABC", 
    "arg2": 1
}

但是,如果我为arg2设置了错误的值,而不是使用错误消息获取状态代码400,则会出现异常:

curl -i  -H "Accept: application/json"  -X PUT --data "arg1=ABC&arg2=A"     http://192.0.0.7:5001/v1.0/test/1
HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: text/html; charset=utf-8
Connection: close
Server: Werkzeug/0.8.3 Python/2.6.6
Date: Fri, 15 Apr 2016 12:04:25 GMT

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>AssertionError // Werkzeug Debugger</title>
    <link rel="stylesheet" href="?__debugger__=yes&amp;cmd=resource&amp;f=style.css" type="text/css">
    <script type="text/javascript" src="?__debugger__=yes&amp;cmd=resource&amp;f=jquery.js"></script>
    <script type="text/javascript" src="?__debugger__=yes&amp;cmd=resource&amp;f=debugger.js"></script>
    <script type="text/javascript">
      var TRACEBACK = 27516816,
          CONSOLE_MODE = false,
          EVALEX = true,
          SECRET = "uovVRKyVTy1b8gi5Yc3t";
    </script>
  </head>
  <body>
    <div class="debugger">
<h1>AssertionError</h1>
<div class="detail">
  <p class="errormsg">AssertionError</p>
</div>
<h2 class="traceback">Traceback <em>(most recent call last)</em></h2>
<div class="traceback">

  <ul><li><div class="frame" id="frame-27516688">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask/app.py"</cite>,
      line <em class="line">1701</em>,
      in <code class="function">__call__</code></h4>
  <pre>return self.wsgi_app(environ, start_response)</pre>
</div>

<li><div class="frame" id="frame-139714215145552">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask/app.py"</cite>,
      line <em class="line">1689</em>,
      in <code class="function">wsgi_app</code></h4>
  <pre>response = self.make_response(self.handle_exception(e))</pre>
</div>

<li><div class="frame" id="frame-139714215148944">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask_restful/__init__.py"</cite>,
      line <em class="line">271</em>,
      in <code class="function">error_router</code></h4>
  <pre>return original_handler(e)</pre>
</div>

<li><div class="frame" id="frame-139714215149072">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask_restful/__init__.py"</cite>,
      line <em class="line">268</em>,
      in <code class="function">error_router</code></h4>
  <pre>return self.handle_error(e)</pre>
</div>

<li><div class="frame" id="frame-139714215149136">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask/app.py"</cite>,
      line <em class="line">1687</em>,
      in <code class="function">wsgi_app</code></h4>
  <pre>response = self.full_dispatch_request()</pre>
</div>

<li><div class="frame" id="frame-139714215149008">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask/app.py"</cite>,
      line <em class="line">1360</em>,
      in <code class="function">full_dispatch_request</code></h4>
  <pre>rv = self.handle_user_exception(e)</pre>
</div>

<li><div class="frame" id="frame-139714215149200">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask_restful/__init__.py"</cite>,
      line <em class="line">271</em>,
      in <code class="function">error_router</code></h4>
  <pre>return original_handler(e)</pre>
</div>

<li><div class="frame" id="frame-139714215149264">
  <h4>File <cite class="filename">"/usr/lib/python2.6/site-packages/flask/app.py"</cite>,
      line <em class="line">1246</em>,
      in <code class="function">handle_user_exception</code></h4>
  <pre>assert exc_value is e</pre>
</div>
</ul>
  <blockquote>AssertionError</blockquote>
</div>

<div class="plain">
  <form action="http://paste.pocoo.org/" method="post">
    <p>
      <input type="hidden" name="language" value="pytb">
      This is the Copy/Paste friendly version of the traceback.  <span
      class="pastemessage">You can also paste this traceback into LodgeIt:
      <input type="submit" value="create paste"></span>
    </p>
    <textarea cols="50" rows="10" name="code" readonly>Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.6/site-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/lib/python2.6/site-packages/flask_restful/__init__.py", line 268, in error_router
    return self.handle_error(e)
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.6/site-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1246, in handle_user_exception
    assert exc_value is e
AssertionError</textarea>
  </form>
</div>
<div class="explanation">
  The debugger caught an exception in your WSGI application.  You can now
  look at the traceback which led to the error.  <span class="nojavascript">
  If you enable JavaScript you can also use additional features such as code
  execution (if the evalex feature is enabled), automatic pasting of the
  exceptions and much more.</span>
</div>
      <div class="footer">
        Brought to you by <strong class="arthur">DON'T PANIC</strong>, your
        friendly Werkzeug powered traceback interpreter.
      </div>
    </div>
  </body>
</html>

<!--

Traceback (most recent call last):
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.6/site-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/lib/python2.6/site-packages/flask_restful/__init__.py", line 268, in error_router
    return self.handle_error(e)
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/lib/python2.6/site-packages/flask_restful/__init__.py", line 271, in error_router
    return original_handler(e)
  File "/usr/lib/python2.6/site-packages/flask/app.py", line 1246, in handle_user_exception
    assert exc_value is e
AssertionError

-->

这一切都在带有

的Centos 6.5上运行
  • Python 2.6.6
  • 烧瓶(0.9)
  • Flask-RESTful(0.3.5)

编辑:如果我使用debug=False运行服务器,我会得到:

$ curl -i  -H "Accept: application/json"  -X PUT --data "arg1=ABC&arg2=A"     http://192.0.0.7:5001/v1.0/test/1 
HTTP/1.0 500 INTERNAL SERVER ERROR
Content-Type: application/json
Content-Length: 37
Server: Werkzeug/0.8.3 Python/2.6.6
Date: Fri, 15 Apr 2016 12:50:37 GMT

{"message": "Internal Server Error"}

1 个答案:

答案 0 :(得分:0)

升级烧瓶至0.10.1,问题消失。