From b83f5257c79e1d87066e5cd90b65a5137d2609b6 Mon Sep 17 00:00:00 2001 From: vincent Date: Sun, 30 May 2021 11:06:15 +0200 Subject: [PATCH] create exeption --- ITPlanning/api/exception.py | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ITPlanning/api/exception.py diff --git a/ITPlanning/api/exception.py b/ITPlanning/api/exception.py new file mode 100644 index 0000000..70a39f5 --- /dev/null +++ b/ITPlanning/api/exception.py @@ -0,0 +1,46 @@ +from flask import request, jsonify +from werkzeug.exceptions import HTTPException + + +class ITPlanningAPIException(HTTPException): + code = 400 + + def __init__(self, message, *args, **kwargs): + super().__init__(*args, **kwargs) + self.message = message + + def get_response(self, environ=None): + rv = jsonify(dict(code=self.code, message=self.message)) + rv.status_code = self.code + return rv + + def __str__(self): + return "{}: {}".format(self.code, self.message) + + +class GenericError(ITPlanningAPIException): + pass + + +class ServerError(ITPlanningAPIException): + code = 500 + + +class Unauthorized(ITPlanningAPIException): + code = 401 + api_code = 40 + message = "Wrong username or password." + + +class Forbidden(ITPlanningAPIException): + code = 403 + api_code = 50 + message = "User is not authorized for the given operation." + + +class NotFound(ITPlanningAPIException): + code = 404 + + def __init__(self, entity, *args, **kwargs): + message = "{} not found".format(entity) + super().__init__(message, *args, **kwargs)