itplanning/ITPlanning/api/exception.py

45 lines
1.0 KiB
Python
Raw Permalink 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
message = "Wrong username or password."
class Forbidden(ITPlanningAPIException):
code = 403
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)