How do I catch an error raised in a decorator?
我正在使用flask auth,它提供一些辅助装饰器。我已经在下面添加了所有不同的方法,但我想问的问题是如何捕获
如果装饰师爆炸了,我怎么能抓住它?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | import os import flask import flask_oauth CONSUMER_KEY = os.environ['CONSUMER_KEY'] CONSUMER_SECRET = os.environ['CONSUMER_SECRET'] oauth = flask_oauth.OAuth() twitter = oauth.remote_app( 'twitter', base_url='https://api.twitter.com/1/', request_token_url='https://api.twitter.com/oauth/request_token', access_token_url='https://api.twitter.com/oauth/access_token', authorize_url='https://api.twitter.com/oauth/authenticate', consumer_key=CONSUMER_KEY, consumer_secret=CONSUMER_SECRET ) app = flask.Flask(__name__) @app.route('/login') def login(): return twitter.authorize( callback=url_for( 'oauth_authorized', next=request.args.get('next') or request.referrer or None) ) @app.route('/oauth-authorized') # what happens if this raises an error? @twitter.authorized_handler def oauth_authorized(resp): print 'foo-bar' |
执行函数定义。因此,假设引发的异常是特定于该修饰符的,则可以将函数定义(包括修饰符)包装在
1 2 3 4 5 6 7 | try: @app.route('/oauth-authorized') @twitter.authorized_handler def oauth_authorized(resp): print 'foo-bar' except WhateverError as e: print"twitter.authorized_handler raised an error", e |
当然,如果引发异常,这将使
或者,由于decorator只是函数(好吧,任何可调用的),而
1 2 3 4 5 6 7 8 | def oauth_authorized(resp): print 'foo-bar' try: # apply decorator oauth_authorized = twitter.authorized_handler(oauth_authorized) except Exception as e: print"twitter.authorized_handler raised an error", e else: # no error decorating with authorized_handler, apply app.route oauth_authorized = app.route('/oauth-authorized')(oauth_authorized) |
号
如果