python - Flask源碼異常處理問(wèn)題
問(wèn)題描述
Flask version: 0.3
我事先在views.py里面定義了如下的異常處理方法:
@app.errorhandler(404)def page_not_found(): return 'this page is not found.', 404 @app.errorhandler(500)def page_error(): return 'this page is error.', 500 #:: 這兩個(gè)異常處理方法都是錯(cuò)誤的,因?yàn)闆](méi)有加參數(shù)。
先貼幾段源碼
#:: 片段1def wsgi_app(self, environ, start_response): with self.request_context(environ):try: rv = self.preprocess_request() if rv is None:rv = self.dispatch_request() response = self.make_response(rv) response = self.process_response(response)except Exception, e: response = self.make_response(self.handle_exception(e))return response(environ, start_response)#:: 片段2def dispatch_request(self): req = _request_ctx_stack.top.request try:if req.routing_exception is not None: raise req.routing_exception #:: 拋出異常return self.view_functions[req.endpoint](**req.view_args) except HTTPException, e:return self.handle_http_exception(e)#:: 片段3class _RequestContext(object): def __init__(self, app, environ):self.app = appself.url_adapter = app.url_map.bind_to_environ(environ)self.request = app.request_class(environ)self.session = app.open_session(self.request)if self.session is None: self.session = _NullSession()self.g = _RequestGlobals()self.flashes = Nonetry: self.request.endpoint, self.request.view_args = self.url_adapter.match() #:: 不匹配except HTTPException, e: print e.code, e.description self.request.routing_exception = e #:: 片段4def handle_exception(self, e): handler = self.error_handlers.get(500) if self.debug:raise self.logger.error('hello') self.logger.exception(’Exception on %s [%s]’ % (request.path,request.method )) if handler is None:return InternalServerError() return handler(e) #:: handler不為None,之前定義了500的錯(cuò)誤處理
然后我在瀏覽器輸入了一個(gè)不存在的路由http://localhost:5000/test,想看Flask如何異常處理。
請(qǐng)求過(guò)來(lái)之后會(huì)先執(zhí)行片段1, 然后代碼走到片段3請(qǐng)求上下文,片段3中self.url_adapter.match()拋出HTTPException異常被捕獲。然后執(zhí)行到片段2,然后拋出異常被片段1捕獲到執(zhí)行片段4,最后會(huì)執(zhí)行到handler(e)這條語(yǔ)句。
我的問(wèn)題來(lái)了,因?yàn)槲叶x的500的錯(cuò)誤處理里面,沒(méi)有加參數(shù),導(dǎo)致這條語(yǔ)句執(zhí)行失敗。
報(bào)了如下錯(cuò)誤:
Traceback (most recent call last): File '/Users/virtualenvs/full-stack/lib/python2.7/site-packages/werkzeug/serving.py', line 180, in run_wsgi execute(self.server.app) File '/Users/virtualenvs/full-stack/lib/python2.7/site-packages/werkzeug/serving.py', line 168, in execute application_iter = app(environ, start_response) File '/Users/virtualenvs/full-stack/flask0.3/examples/flaskr/flask.py', line 1435, in __call__ return self.wsgi_app(environ, start_response) File '/Users/virtualenvs/full-stack/lib/python2.7/site-packages/werkzeug/wsgi.py', line 591, in __call__ return self.app(environ, start_response) File '/Users/virtualenvs/full-stack/flask0.3/examples/flaskr/flask.py', line 1382, in wsgi_app response = self.make_response(self.handle_exception(e)) File '/Users/virtualenvs/full-stack/flask0.3/examples/flaskr/flask.py', line 1266, in handle_exception return handler(e)TypeError: page_error() takes no arguments (1 given)
從Traceback當(dāng)中能看出最后也是這句的錯(cuò)誤。然而瀏覽器還是返回了500的頁(yè)面,返回的內(nèi)容是Werkzeug自定義的頁(yè)面內(nèi)容。
Internal Server ErrorThe server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
既然程序都已經(jīng)執(zhí)行錯(cuò)誤了,那為什么還能夠返回HTTP Response呢?謝謝:-)
問(wèn)題解答
回答1:報(bào)錯(cuò)信息提示很清楚 File '/Users/virtualenvs/full-stack/flask0.3/examples/flaskr/flask.py', line 1266, in handle_exception
return handler(e)
TypeError: page_error() takes no arguments (1 given)
page_error() 不需要參數(shù),你傳遞了一個(gè)參數(shù)。
相關(guān)文章:
1. html5 - H5實(shí)現(xiàn)微場(chǎng)景的思路是什么 環(huán)境怎么搭建 求大神幫忙帶路 有好的課程希望推薦一下2. css3 - <img>圓角屬性在手機(jī)瀏覽器顯示不全3. css - label文字居中4. javascript - js輸入框限定字?jǐn)?shù)問(wèn)題5. pdo - mysql 簡(jiǎn)單注入疑問(wèn)6. javascript - vue項(xiàng)目里的package.json7. python - 關(guān)于爬取網(wǎng)站,下載圖片的時(shí)候碰到網(wǎng)址結(jié)構(gòu)問(wèn)題卡住8. 引入traits后,為什么index得是空的呢?9. javascript - table固定尾行,有人寫過(guò)嗎?10. 為什么學(xué)習(xí)PHP
