🎉 init starlette dial
This commit is contained in:
82
pkg/resp/exception.py
Normal file
82
pkg/resp/exception.py
Normal file
@@ -0,0 +1,82 @@
|
||||
from starlette.requests import Request
|
||||
from starlette.responses import JSONResponse
|
||||
|
||||
|
||||
class BadRequestException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 400
|
||||
self.msg = msg or "参数错误"
|
||||
self.data = data
|
||||
self.err = err
|
||||
super().__init__(self.msg)
|
||||
|
||||
|
||||
class ServerErrorException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 500
|
||||
self.msg = msg or "服务器开小差了"
|
||||
self.data = data
|
||||
self.err = err
|
||||
super().__init__(self.msg)
|
||||
|
||||
|
||||
class UnauthorizationException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 401
|
||||
self.msg = msg or "登录信息不存在或已过期, 请重新登录"
|
||||
self.data = data
|
||||
self.err = err
|
||||
super().__init__(self.msg)
|
||||
|
||||
|
||||
class ForbiddenException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 403
|
||||
self.msg = msg or "权限不足"
|
||||
self.data = data
|
||||
self.err = err
|
||||
super().__init__(self.msg)
|
||||
|
||||
|
||||
class NotFoundException(Exception):
|
||||
def __init__(self, msg="", data=None, err=None, status=None):
|
||||
self.status = status or 404
|
||||
self.msg = msg or "资源不存在"
|
||||
self.data = data
|
||||
self.err = err
|
||||
super().__init__(self.msg)
|
||||
|
||||
|
||||
def server_error_exception_handler(request: Request, exc):
|
||||
return JSONResponse(
|
||||
{"msg": exc.msg, "data": exc.data, "err": exc.err, "status": exc.status},
|
||||
status_code=500,
|
||||
)
|
||||
|
||||
|
||||
def unauthorization_exception_handler(request: Request, exc):
|
||||
return JSONResponse(
|
||||
{"msg": exc.msg, "data": exc.data, "err": exc.err, "status": exc.status},
|
||||
status_code=401,
|
||||
)
|
||||
|
||||
|
||||
def forbidden_exception_handler(request: Request, exc):
|
||||
return JSONResponse(
|
||||
{"msg": exc.msg, "data": exc.data, "err": exc.err, "status": exc.status},
|
||||
status_code=403,
|
||||
)
|
||||
|
||||
|
||||
def bad_request_exception_handler(request: Request, exc):
|
||||
return JSONResponse(
|
||||
{"msg": exc.msg, "data": exc.data, "err": exc.err, "status": exc.status},
|
||||
status_code=400,
|
||||
)
|
||||
|
||||
|
||||
def not_found_exception_handler(request: Request, exc):
|
||||
return JSONResponse(
|
||||
{"msg": exc.msg, "data": exc.data, "err": exc.err, "status": exc.status},
|
||||
status_code=404,
|
||||
)
|
||||
Reference in New Issue
Block a user