Flasche-RESTful: Warum bekomme ich einen AssertionError beim Analysieren eines Arguments mit dem falschen Typ?

Ich bin mit Flasche-RESTful zum ersten Mal. In den docs heißt es:

Mit dem reqparse-Modul gibt es auch gesunde Fehlermeldungen kostenlos. Wenn ein Argument die Validierung nicht bestanden hat, antwortet Flask-RESTful mit einer 400 schlechten Anforderung und einer Antwort, die den Fehler hervorhebt.

In meinem Fall bekomme ich stattdessen eine Ausnahme AssertionError.

Hier ist der 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) 

Wenn ich das mit richtigen Werten teste, funktioniert es:

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

Allerdings, wenn ich einen falschen Wert für arg2, anstatt einen Statuscode 400 mit einer Fehlermeldung bekomme ich eine Ausnahme:

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

Das läuft alles auf einem Centos 6.5 mit

  • Python 2.6.6
  • Flasche (0,9)
  • Flasche-RESTful (0.3.5)

EDIT: Wenn ich den Server mit debug=False ausführe, bekomme ich:

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

One Solution collect form web for “Flasche-RESTful: Warum bekomme ich einen AssertionError beim Analysieren eines Arguments mit dem falschen Typ?”

Verbesserte Flasche auf 0.10.1 und das Problem verschwindet.

  • Flask-Login hebt TypeError: 'bool' Objekt ist beim Aufrufen der is_active-Eigenschaft nicht abrufbar
  • Wie fügt man Felder URL für verschachtelte Ausgabefelder in der Klappe auf
  • Wie kann ich bei der Anzeige von Fremdschlüssel-Feldern eine Flasche-Admin verwenden?
  • Flasche sieht keine Änderung in .js-Datei
  • Flaschenansicht hebt TypeError: 'bool' Objekt ist nicht abrufbar
  • Sprache ändern Flasche Babel manuell
  • Spezifischere SQL-Abfrage mit flask-wtf queryselectfield
  • Wie man benutzerdefinierten Code bei der Anmeldung mit Flasche-Sicherheit ausführt
  • Flask-admin is_accessible Nutzung
  • Flask-SQLAlchemy: Wie kann man eine Zeile bedingt einfügen oder aktualisieren?
  • Flask-login AttributeError: 'User' Objekt hat kein Attribut 'is_active'
  • Python ist die beste Programmiersprache der Welt.