flask-RESTful:解析一个错误类型的参数时为什么会得到一个AssertionError? [英] flask-RESTful : why do I get an AssertionError when parsing an argument with the wrong type?

查看:1842
本文介绍了flask-RESTful:解析一个错误类型的参数时为什么会得到一个AssertionError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用flask-RESTful。在文档中说:


使用reqparse模块还可以免费提供完整的错误消息。如果一个参数未能通过验证,Flask-RESTful会响应一个400错误请求和一个突出显示错误的响应。


我的情况,我得到一个异常AssertionError而不是。



这是代码:

 #! / usr / bin / python 
# - * - 编码:UTF-8 - * -

从flask导入Flask
from flask.ext.restful import Api,Resource,reqparse

$ b class Test(Resource):
@staticmethod
def put(id):
parser = reqparse.RequestParser()
解析器.add_argument('arg1')
parser.add_argument('arg2',type = int,help ='helptext')
args = parser.parse_args()
返回参数,200
$ b $ 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 -HAccept:application / json-X PUT --dataarg1 = ABC& arg2 = 1http://192.0.0.7:5001/ v1.0 / test / 1 
HTTP / 1.0 200 OK
Content-Type:application / json
Content-Len gth:38
服务器:Werkzeug / 0.8.3 Python / 2.6.6
日期:2016年4月15日星期五11:59:48 GMT

{
arg1:ABC,
arg2:1
}

然而,如果我把一个错误的值arg2,而不是得到一个错误消息的状态代码400,我得到一个异常:

  curl -i -HAccept:application / json-X PUT --dataarg1 = ABC& arg2 = Ahttp://192.0.0.7:5001/v1.0/test/1 
HTTP / 1.0 500内部服务器错误
Content-Type:text / html; charset = utf-8
连接:关闭
服务器:Werkzeug / 0.8.3 Python / 2.6.6
日期:2016年4月15日星期五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 =stylesheethref =?__ debugger __ = yes& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;
< script type =text / javascriptsrc =?__ debugger __ = yes& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;
< script type =text / javascriptsrc =?__ debugger __ = yes& amp; amp; cmd = resource& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp;
< 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>(最近最后一次致电)< / em>< / h2>
< div class =traceback>

< ul>< li>< div class =frameid =frame-27516688>

文件< cite class =filename>/ usr / lib / python2.6 / site-packages / flask / app.py< / cite> ;,
line < em class =line> 1701< / em> ;,
在< code class =function> __ call __< / code>< / h4>
< pre>返回self.wsgi_app(environ,start_response)< / pre>
< / div>

< li>< div class =frameid =frame-139714215145552>

文件< cite class =filename>/ usr / lib / python2.6 / site-packages / flask / app.py< / cite> ;,
line < em class =line> 1689< / em> ;,
位于< code class =function> wsgi_app< / code>< / h4>
< pre> response = self.make_response(self.handle_exception(e))< / pre>
< / div>

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

< li>< div class =frameid =frame-139714215149072>
< h4>文件< cite class =filename>/ usr / lib / python2.6 / site-packages / flask_restful / __init__.py< / cite> ;,
line < em class =line> 268< / em> ;,
在< code class =function> error_router< / code>< / h4>
< pre>返回self.handle_error(e)< / pre>
< / div>

< li>< div class =frameid =frame-139714215149136>

文件< cite class =filename>/ usr / lib / python2.6 / site-packages / flask / app.py< / cite> ;,
line < em class =line> 1687< / em> ;,
位于< code class =function> wsgi_app< / code>< / h4>
< pre> response = self.full_dispatch_request()< / pre>
< / div>

< li>< div class =frameid =frame-139714215149008>

文件< cite class =filename>/ usr / lib / python2.6 / site-packages / flask / app.py< / cite> ;,
line < em class =line> 1360< / em> ;,
在< code class =function> full_dispatch_request< / code>< / h4>
< pre> rv = self.handle_user_exception(e)< / pre>
< / div>

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

< li>< div class =frameid =frame-139714215149264>

文件< cite class =filename>/ usr / lib / python2.6 / site-packages / flask / app.py< / cite> ;,
line < em class =line> 1246< / em> ;,
在< 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 =hiddenname =languagevalue =pytb>
这是回溯的复制/粘贴友好版本。 < span
class =pastemessage>您也可以将此回溯贴到LodgeIt:
< input type =submitvalue =create paste>< / span>
< / p>
< textarea cols =50rows =10name =codereadonly> Traceback(最近一次调用最后一次):
/usr/lib/python2.6/site-packages /flask/app.py,第1701行,在__call__
中返回self.wsgi_app(environ,start_response)
文件/usr/lib/python2.6/site-packages/flask/app.py ,第1689行,在wsgi_app
response = self.make_response(self.handle_exception(e))
文件/usr/lib/python2.6/site-packages/flask_restful/__init__.py,第271行,在error_router
返回original_handler(e)
文件/usr/lib/python2.6/site-packages/flask_restful/__init__.py,第268行,在error_router
返回self.handle_error(e)
文件/usr/lib/python2.6/site-packages/flask/app.py,第1687行,位于wsgi_app
response = self.full_dispatch_request()
文件/usr/lib/python2.6/site-packages/flask/app.py,第1360行,在full_dispatch_request
rv = self.handle_user_exception(e)
文件/ usr / LIB / python2.6的/站点包/ flask_restful / __中it__.py,第271行,在error_router
中返回original_handler(e)
文件/usr/lib/python2.6/site-packages/flask/app.py,行1246,在handle_user_exception
断言exc_value是e
AssertionError< / textarea>
< / form>
< / div>
< div class =explanation>
调试器在您的WSGI应用程序中发现异常。你现在可以
看看导致错误的回溯。 < span class =nojavascript>
如果您启用JavaScript,您还可以使用其他功能,例如代码
执行(如果已启用evalex功能),自动粘贴
例外等。< / span>
< / div>
< div class =footer>
由< strong class =arthur> DO NOT PANIC< / strong>,您的
友好的Werkzeug支持的追踪解释器带给您。
< / div>
< / div>
< / body>
< / html>

<! -

Traceback(最近的最后一次调用):
文件/usr/lib/python2.6/site-packages/flask/ app.py,行1701,在__call__
返回self.wsgi_app(environ,start_response)
文件/usr/lib/python2.6/site-packages/flask/app.py,行1689,在wsgi_app
response = self.make_response(self.handle_exception(e))
文件/usr/lib/python2.6/site-packages/flask_restful/__init__.py,第271行,在error_router
返回original_handler(e)
文件/usr/lib/python2.6/site-packages/flask_restful/__init__.py,第268行,在error_router
返回self.handle_error (e)
文件/usr/lib/python2.6/site-packages/flask/app.py,行1687,位于wsgi_app
response = self.full_dispatch_request()
文件/usr/lib/python2.6/site-packages/flask/app.py,第1360行,在full_dispatch_request
rv = self.handle_user_exception(e)
文件/ usr / lib / python2 .6 / site-packages / flask_restful / __init__.py,第271行,在error_router中
返回original_handler(e)
文件/usr/lib/python2.6/site-packages/flask/app.py,行1246,在handle_user_exception中
assert exc_value是e
AssertionError

- >

这些都是在Centos 6.5上运行的,包含


  • Python 2.6.6

  • Flask(0.9)

  • Flask-RESTful(0.3.5) li>


编辑:如果我用 debug = False 运行服务器, / p>

  $ curl -i -HAccept:application / json-X PUT --dataarg1 = ABC& arg2 = A http://192.0.0.7:5001/v1.0/test/1 
HTTP / 1.0 500内部服务器错误
Content-Type:application / json
Content-Length:37
服务器:Werkzeug / 0.8.3 Python / 2.6.6
日期:2016年4月15日星期五12:50:37 GMT

{message:内部服务器错误解决方案

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


I'm using flask-RESTful for the first time. In the docs it says :

Using the reqparse module also gives you sane error messages for free. If an argument fails to pass validation, Flask-RESTful will respond with a 400 Bad Request and a response highlighting the error.

However in my case, I get an exception AssertionError instead.

Here's the code:

#! /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)

When I test this with right values, it works:

$ 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
}

However if I put a wrong value for arg2, instead of getting a status code 400 with an error message I get an exception:

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

-->

This is all running on a Centos 6.5 with

  • Python 2.6.6
  • Flask (0.9)
  • Flask-RESTful (0.3.5)

EDIT: If I run the server with debug=False, I get:

$ 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"}

解决方案

Upgraded flask to 0.10.1 and the problem disappears.

这篇关于flask-RESTful:解析一个错误类型的参数时为什么会得到一个AssertionError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆