From b233a7cac73d0c66b988ac62adfccf4ccb26141d Mon Sep 17 00:00:00 2001 From: Rich Date: Fri, 22 Sep 2023 17:28:42 +0200 Subject: [PATCH 1/2] Log startup errors with a full stack trace --- web.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web.py b/web.py index 813c9b2..905cb2c 100644 --- a/web.py +++ b/web.py @@ -29,8 +29,8 @@ try: module_path = 'ext.app.{}'.format(app_file) import_module(module_path) -except Exception as e: - helpers.log(str(e)) +except Exception: + helpers.logger.exception('Exception raised when importing app code') ####################### ## Start Application ## From f2e2ae0599de933a45a4b6e33bd4bbc6340214e9 Mon Sep 17 00:00:00 2001 From: Rich Date: Fri, 22 Sep 2023 17:29:08 +0200 Subject: [PATCH 2/2] Add env var to add an exception logging errorhandler --- README.md | 2 ++ web.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/README.md b/README.md index d71e2d0..0c1eaae 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,8 @@ my-python: - `MODE` to specify the deployment mode. Can be `development` as well as `production`. Defaults to `production` +- `LOG_EXCEPTIONS` set to any value to add a catch-all Flask errorhandler that logs exceptions rather than just returning them, to aid with debugging. Is not on by default in development as it may prevent custom errorhandlers in you app from being reached. + - `MU_SPARQL_ENDPOINT` is used to configure the SPARQL endpoint. - By default this is set to `http://database:8890/sparql`. In that case the triple store used in the backend should be linked to the microservice container as `database`. diff --git a/web.py b/web.py index 905cb2c..a5cb7e9 100644 --- a/web.py +++ b/web.py @@ -32,6 +32,12 @@ except Exception: helpers.logger.exception('Exception raised when importing app code') +if os.environ.get('LOG_EXCEPTIONS'): + @app.errorhandler(Exception) + def handle_exception(e): + helpers.logger.exception('Unhandled exception raised in route, returning 500') + raise e + ####################### ## Start Application ## #######################