Python:如何在我的Flask应用程序中获取导致错误的行?

时间:2014-02-15 16:46:41

标签: python numpy flask stack-trace

我正在使用Flask建立一个网站。我现在遇到了一个错误,它给出了一个回溯,不知道我自己编写的代码的哪一部分导致了错误:

Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/Library/Python/2.7/site-packages/Flask-0.10-py2.7.egg/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/wrappers.py", line 824, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/Library/Python/2.7/site-packages/Werkzeug-0.9.4-py2.7.egg/werkzeug/test.py", line 854, in run_wsgi_app
    app_iter = app(environ, start_response)
TypeError: 'numpy.float64' object is not callable

我确实在我的代码中使用了numpy,但是看到这个堆栈跟踪,我不知道在哪里需要开始在我自己的代码中查找错误。

[编辑] 感谢您的评论。我发现了我的错误;我确实回到了一个凹凸不平的漂浮物。

它并没有解决我有时与Flask有关的根本问题;我在哪里可以找到导致错误的代码行?为方便起见,为什么堆栈跟踪不包含我自己脚本中的行。有没有办法让我总能在自己的脚本中返回导致错误的行?当然我们这里都是技术人员,但我认为对于Flask的新手来说,这是一个非常非常非常重要的功能。

所以我把问题的标题改回原来的样子,因为这实际上是这里的基本问题。

欢迎任何提示!

1 个答案:

答案 0 :(得分:0)

在这里,我调整了第一个flask脚本以产生一些错误:

from flask import Flask
app = Flask(__name__)
app.debug = True

@app.route("/")
def hello():
    return "Hello World!"

@app.route("/err")
def bad():
    return 1.234

@app.route("/debugged")
def debugged():
    resp = 1.234
    assert isinstance(resp,str), 'response must be string'
    return resp

if __name__ == "__main__":
    app.run()

当我运行它时,服务器显示以下日志(在生产中,这可能会转到日志文件)。 500行告诉我错误是由/err调用产生的。

127.0.0.1 - - [16/Feb/2014 09:53:18] "GET /err HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
...
  File "/usr/local/lib/python2.7/site-packages/werkzeug/test.py", line 854, in run_wsgi_app
    app_iter = app(environ, start_response)
TypeError: 'float' object is not callable

我的bad()函数没有出现在堆栈上,因为它成功运行了。这是flask代码使用其响应引发错误。但我可以通过跟踪路由来识别我的功能。

如果我调用具有assert防护的应用程序,我会得到这样的日志记录:

127.0.0.1 - - [16/Feb/2014 09:56:03] "GET /debugged HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
...
  File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/paul/mypy/stack21800569.py", line 13, in debugged
    assert isinstance(resp,str), 'response must be string'
AssertionError: response must be string

现在我可以看到我的debugged()函数试图产生错误的响应。

我还可以添加日志记录:

@app.route("/logged")
def logged():
    resp = '1.234'
    app.logger.debug('logger function response: %s'%resp)
    return resp

产生

--------------------------------------------------------------------------------
DEBUG in stack21800569 [stack21800569.py:19]:
logger function response: 1.234
--------------------------------------------------------------------------------
127.0.0.1 - - [16/Feb/2014 12:00:51] "GET /logged HTTP/1.1" 200 -