From 719d605e79fd548f1920e6ae4b51723db4d12e6b Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 30 May 2021 11:05:19 +0200 Subject: [PATCH] add errors handling --- ITPlanning/api/__init__.py | 3 ++- ITPlanning/api/errors.py | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 ITPlanning/api/errors.py diff --git a/ITPlanning/api/__init__.py b/ITPlanning/api/__init__.py index 6bc74ac..6e5a747 100644 --- a/ITPlanning/api/__init__.py +++ b/ITPlanning/api/__init__.py @@ -1,7 +1,8 @@ from flask import Blueprint -api = Blueprint("api",__name__) +api = Blueprint("api", __name__) +from .errors import * from .appointement import * from .planning import * from .service import * diff --git a/ITPlanning/api/errors.py b/ITPlanning/api/errors.py new file mode 100644 index 0000000..cd9cf56 --- /dev/null +++ b/ITPlanning/api/errors.py @@ -0,0 +1,22 @@ +from pony.orm import rollback +from pony.orm import ObjectNotFound +from .exception import ServerError, NotFound, GenericError +from . import api + + +@api.errorhandler(ValueError) +def value_error(e): + rollback() + return GenericError("{0.__class__.__name__}: {0}".format(e)) + + +@api.errorhandler(ObjectNotFound) +def object_not_found(e): + rollback() + return NotFound(e.entity.__name__) + + +@api.errorhandler(500) +def generic_error(e): # pragma: nocover + rollback() + return ServerError("{0.__class__.__name__}: {0}".format(e))