瓶尊重wsgi規範。您可以使用經典的WSGI中間件
from bottle import route, default_app, run, request
# push an application in the AppStack
default_app.push()
@route('/foo')
def foo():
raise KeyError()
# error view
@route('/error')
def error():
return 'Sorry an error occured %(myapp.error)r' % request.environ
# get the bottle application. can be a Bottle() instance too
app = default_app.pop()
app.catchall = False
def error_catcher(environ, start_response):
# maybe better to fake the start_response callable but this work
try:
return app.wsgi(environ, start_response)
except Exception as e:
# redirect to the error view if an exception is raised
environ['PATH_INFO'] = '/error'
environ['myapp.error'] = e
return app.wsgi(environ, start_response)
# serve the middleware instead of the applicatio
run(app=error_catcher)
難道你根本無法從派生異常bottle.HTTPResponse與異常類型,然後做適當的事情開始或是你的異常的來源,而不是你的Web應用程序的一部分,因此不是已經依賴瓶子? –
從獨立庫中拋出異常;瓶子只是它的一個前端。 – stackoverflowuser95
會[Bottle插件](http://bottlepy.org/docs/dev/plugindev.html)嗎? –