Skip to content

Commit c0565ab

Browse files
author
Hugo Camino
committed
Added methods for error handler registration in Microservice's subclasses.
1 parent e552a8e commit c0565ab

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

pyms/flask/app/__init__.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class Microservice:
1515
service = None
16+
application = None
1617

1718
def __init__(self, service: Text, path=__file__):
1819
self.service = service
@@ -35,23 +36,42 @@ def create_app(self):
3536
base_path=config.APPLICATION_ROOT
3637
)
3738

38-
application = app.app
39-
application.config.from_object(get_conf(service=self.service))
40-
application.tracer = None
39+
self.application = app.app
40+
self.application._connexion_app = app
41+
self.application.config.from_object(get_conf(service=self.service))
42+
self.application.tracer = None
4143

4244
# Initialize Blueprints
43-
application.register_blueprint(healthcheck_blueprint)
44-
self.init_libs(application)
45+
self.application.register_blueprint(healthcheck_blueprint)
46+
47+
self.init_libs(self.application)
48+
self.add_error_handlers()
49+
4550
# Inject Modules
4651
formatter = CustomJsonFormatter('(timestamp) (level) (name) (module) (funcName) (lineno) (message)')
47-
if not application.config["TESTING"]:
52+
if not self.application.config["TESTING"]:
4853
log_handler = logging.StreamHandler()
4954

50-
application.tracer = FlaskTracer(init_jaeger_tracer(), True, application)
51-
formatter.add_service_name(application.config["APP_NAME"])
52-
formatter.add_trace_span(application.tracer)
55+
self.application.tracer = FlaskTracer(init_jaeger_tracer(), True, self.application)
56+
formatter.add_service_name(self.application.config["APP_NAME"])
57+
formatter.add_trace_span(self.application.tracer)
5358
log_handler.setFormatter(formatter)
54-
application.logger.addHandler(log_handler)
55-
application.logger.setLevel(logging.INFO)
59+
self.application.logger.addHandler(log_handler)
60+
self.application.logger.setLevel(logging.INFO)
61+
62+
return self.application
63+
64+
def add_error_handlers(self):
65+
"""Subclasses will override this method in order to add specific error handlers. This should be done with
66+
calls to add_error_handler method.
67+
"""
68+
pass
69+
70+
def add_error_handler(self, code_or_exception, handler):
71+
"""Add custom handler for an error code or exception in the connexion app.
72+
73+
:param code_or_exception: HTTP error code or exception
74+
:param handler: callback for error handler
75+
"""
5676

57-
return application
77+
self.application._connexion_app.add_error_handler(code_or_exception, handler)

0 commit comments

Comments
 (0)