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))