def register(self): """ Register the service provider """ # Construct and define handler handler_class = self.app.config('app.exceptions.handler', Handler) handler = handler_class(self.app) self.app.extensions['edmunds.exceptions.handler'] = handler # Add all the exception to handle exceptions = list(default_exceptions.values()) exceptions.append(Exception) # Register each exception for exception_class in exceptions: @self.app.errorhandler(exception_class) def handle_exception(exception): """ Handle an exception :param exception: The exception :type exception: Exception :return: The response """ self.app.extensions['edmunds.exceptions.handler'].report( exception) return self.app.extensions[ 'edmunds.exceptions.handler'].render(exception)
def init_error_handlers(app): for exception in default_exceptions.values(): logger.debug(f"Registering handler for {exception}") app.register_error_handler(exception, error) if not app.config.get("TESTING", False): app.register_error_handler(Exception, error)
def test_http_exceptions(self): """ Test http exceptions """ rule = '/' + self.rand_str(20) abort_exception = None self.write_config([ "from tests.exceptions.testhandler import MyHandler \n", "APP = { \n", " 'debug': False, \n", " 'exceptions': { \n", " 'handler': MyHandler \n", " } \n", "} \n", ]) app = self.create_application() # Add route @app.route(rule) def handle_route(): TestHandler.cache['timeline'].append('handle_route') abort(abort_exception) return 'handled route' # Check current handler self.assert_equal(MyHandler, app.config('app.exceptions.handler', None)) # Call route with app.test_client() as c: # Loop http exceptions for http_exception in default_exceptions.values(): abort_exception = http_exception.code TestHandler.cache = dict() TestHandler.cache['timeline'] = [] rv = c.get(rule) # THIS IS NOT OK! See https://github.com/pallets/werkzeug/issues/1231 # Waiting for fix to be released. if http_exception.code != 412: self.assert_equal('rendered', rv.get_data(True)) self.assert_equal(http_exception.code, rv.status_code) self.assert_equal(4, len(TestHandler.cache['timeline'])) self.assert_in('handle_route', TestHandler.cache['timeline']) self.assert_equal(0, TestHandler.cache['timeline'].index('handle_route')) self.assert_in(MyHandler.__name__ + '.report', TestHandler.cache['timeline']) self.assert_equal(1, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.report')) self.assert_in(MyHandler.__name__ + '.render', TestHandler.cache['timeline']) self.assert_equal(2, TestHandler.cache['timeline'].index(MyHandler.__name__ + '.render')) self.assert_true(isinstance(TestHandler.cache['timeline'][3], HTTPException)) self.assert_equal(http_exception.code, TestHandler.cache['timeline'][3].code)
def init_app(self, app): app.extensions['resty'] = FlaskRestyState(self) app.register_error_handler(ApiError, handle_api_error) # TODO: Just handle HTTPException once pallets/flask#2314 lands. for exception in default_exceptions.values(): app.register_error_handler(exception, handle_http_exception)
def get_errors(error_name): try: error_dict = { error.__name__: error for error in default_exceptions.values() } raise error_dict[error_name](description=TEST_MESSAGE) except KeyError: raise TypeError(TEST_MESSAGE)
def register_error_handlers(app): '''register all error handlers for this flask app''' # http://flask.pocoo.org/docs/0.12/api/#flask.Flask.register_error_handler # handler for python generic "Exceptions" app.register_error_handler(Exception, handle_exception) # explicitly handle all werkzeug exceptions for err in default_exceptions.values(): app.register_error_handler(err, handle_werkzeug_exception)
def isFlaskException(exception): if type(exception) in default_exceptions.values(): return True else: return False
def configure_flask_app(app): for exception_type in default_exceptions.values(): app.register_error_handler(exception_type, _flask_json_errorhandler)
def initialize_error_handlers(app): _register(app, HTTPException) for exception_type in default_exceptions.values(): _register(app, exception_type)