From ed9fc266c00882910313f6ff00940eeff2f1f650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADn=20Sempere?= Date: Wed, 5 Oct 2022 12:56:03 +0200 Subject: [PATCH] #312 update error handler to handle different type of exceptions --- CHANGELOG.md | 2 ++ prom2teams/app/api.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a8ccde1..3a51805 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a changelog](https://github.com/olivierlacan/keep-a-changelog). ## [Unreleased](https://github.com/idealista/prom2teams/tree/develop) +### Added +- *[#312](https://github.com/idealista/prom2teams/pull/312) Improve error handler to distinguish between different kind of exceptions* @smartinsempere ## [4.1.0](https://github.com/idealista/prom2teams/tree/4.1.0) [Full Changelog](https://github.com/idealista/prom2teams/compare/4.0.0...4.1.0) ### Fixed diff --git a/prom2teams/app/api.py b/prom2teams/app/api.py index e19f99c..edf7121 100644 --- a/prom2teams/app/api.py +++ b/prom2teams/app/api.py @@ -2,8 +2,10 @@ import os from flask import Flask, Blueprint, send_from_directory +from marshmallow.exceptions import ValidationError from prom2teams.app.configuration import config_app, setup_logging +from .exceptions import MicrosoftTeamsRequestException from .versions.v1 import api_v1 from .versions.v1.namespace import ns as ns_v1 from .versions.v2 import api_v2 @@ -13,26 +15,33 @@ app = Flask(__name__) + @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon') + @app.route('/alive') def alive(): return "YES", 200 + @app.route('/ready') def ready(): return ("YES", 200) if app.config['FINISH_INIT'] else ("NO", 503) + def error_handler(e): msg = 'An unhandled exception occurred. {}'.format(e) log.exception(msg) + if isinstance(e, MicrosoftTeamsRequestException): + return str(e), e.code + if isinstance(e, ValidationError): + return str(e), 400 return str(e), 500 - def register_api(application, api, namespace, blueprint): api.init_app(blueprint) api.add_namespace(namespace) @@ -50,5 +59,6 @@ def init_app(application): application.register_error_handler(500, error_handler) application.config['FINISH_INIT'] = True + init_app(app) log.info('{} started on {}:{}'.format(app.config['APP_NAME'], app.config['HOST'], app.config['PORT']))