From c0565ab7afa51495dbcdb69c7d551906d4b31c7a Mon Sep 17 00:00:00 2001 From: Hugo Camino Date: Thu, 29 Nov 2018 10:41:51 +0100 Subject: [PATCH 1/2] Added methods for error handler registration in Microservice's subclasses. --- pyms/flask/app/__init__.py | 44 +++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/pyms/flask/app/__init__.py b/pyms/flask/app/__init__.py index 0b25fe7..4a4546c 100644 --- a/pyms/flask/app/__init__.py +++ b/pyms/flask/app/__init__.py @@ -13,6 +13,7 @@ class Microservice: service = None + application = None def __init__(self, service: Text, path=__file__): self.service = service @@ -35,23 +36,42 @@ def create_app(self): base_path=config.APPLICATION_ROOT ) - application = app.app - application.config.from_object(get_conf(service=self.service)) - application.tracer = None + self.application = app.app + self.application._connexion_app = app + self.application.config.from_object(get_conf(service=self.service)) + self.application.tracer = None # Initialize Blueprints - application.register_blueprint(healthcheck_blueprint) - self.init_libs(application) + self.application.register_blueprint(healthcheck_blueprint) + + self.init_libs(self.application) + self.add_error_handlers() + # Inject Modules formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)') - if not application.config["TESTING"]: + if not self.application.config["TESTING"]: log_handler = logging.StreamHandler() - application.tracer = FlaskTracer(init_jaeger_tracer(), True, application) - formatter.add_service_name(application.config["APP_NAME"]) - formatter.add_trace_span(application.tracer) + self.application.tracer = FlaskTracer(init_jaeger_tracer(), True, self.application) + formatter.add_service_name(self.application.config["APP_NAME"]) + formatter.add_trace_span(self.application.tracer) log_handler.setFormatter(formatter) - application.logger.addHandler(log_handler) - application.logger.setLevel(logging.INFO) + self.application.logger.addHandler(log_handler) + self.application.logger.setLevel(logging.INFO) + + return self.application + + def add_error_handlers(self): + """Subclasses will override this method in order to add specific error handlers. This should be done with + calls to add_error_handler method. + """ + pass + + def add_error_handler(self, code_or_exception, handler): + """Add custom handler for an error code or exception in the connexion app. + + :param code_or_exception: HTTP error code or exception + :param handler: callback for error handler + """ - return application + self.application._connexion_app.add_error_handler(code_or_exception, handler) From 11c372a987b497ce969f6c154249c53c2081e047 Mon Sep 17 00:00:00 2001 From: Hugo Camino Date: Thu, 29 Nov 2018 11:02:44 +0100 Subject: [PATCH 2/2] Removing unnecessary param. --- pyms/flask/app/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pyms/flask/app/__init__.py b/pyms/flask/app/__init__.py index 4a4546c..be42a33 100644 --- a/pyms/flask/app/__init__.py +++ b/pyms/flask/app/__init__.py @@ -19,8 +19,8 @@ def __init__(self, service: Text, path=__file__): self.service = service self.path = os.path.dirname(path) - def init_libs(self, app): - return app + def init_libs(self): + return self.application def create_app(self): """Initialize the Flask app, register blueprints and initialize @@ -44,7 +44,7 @@ def create_app(self): # Initialize Blueprints self.application.register_blueprint(healthcheck_blueprint) - self.init_libs(self.application) + self.init_libs() self.add_error_handlers() # Inject Modules