itplanning/ITPlanning/api/exception.py

47 lines
1.1 KiB
Python
Raw Normal View History

2021-05-30 09:06:15 +00:00
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)