Skip to content

Commit

Permalink
Merge pull request #313 from idealista/features/312
Browse files Browse the repository at this point in the history
#312 update error handler to handle different type of exceptions
  • Loading branch information
smartinsempere committed Oct 5, 2022
2 parents 0517475 + ed9fc26 commit 0c4203b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion prom2teams/app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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']))

0 comments on commit 0c4203b

Please sign in to comment.