- 分享一个前后端分离(Python3 + Flask +Vuew)的个人博客,一个使用Python Flask 框架编写的个人博客。项目很轻。却很实用。
- GitHub地址:哆啦瞎梦
- 技术栈:Flask、Vue、Docker
- 初始化,项目结构我就不再写了,之前写过了,没有的可以看我之前的文章。或者直接私信我,我特地准备了一个干净的模板
# -*- coding: utf-8 -*-
class ErrorCode:
def __init__(self):
pass
Debug = 0
Info = 1
Warn = 2
Error = 3
class AppError(Exception):
"""
统一应用错误对象,返回错误编码和消息
"""
def __init__(self, msg="you've got a error!", code=ErrorCode.Warn, error_code=0):
self.code = code
self.msg = msg
self.error_code = error_code
def __str__(self):
return self.msg
class ParaValidateFailError(AppError):
"""
参数验证失败
"""
def __init__(self, msg="you've got a error!", code=400, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
class NotFoundError(AppError):
"""
缺失错误
"""
def __init__(self, msg="you've got a error!", code=404, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
class UnauthorizedError(AppError):
"""
鉴权错误
"""
def __init__(self, msg="you've got a error!", code=401, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
class ValidationError(AppError):
"""
参数错误
"""
def __init__(self, msg="you've got a error!", code=400, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
class MoreValidationError(AppError):
"""
参数多出错误
"""
def __init__(self, msg="you've got a error!", code=400, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
class ConflictError(AppError):
def __init__(self, msg="you've got a error!", code=409, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
class ServerError(AppError):
def __init__(self, msg="you've got a error!", code=500, error_code=None):
self.code = code
self.msg = msg
self.error_code = error_code
from werkzeug.exceptions import HTTPException
class APIException(HTTPException):
msg = 'sorry, we make a mistake'
def __init__(self, msg=None, headers=None):
if msg:
self.msg = msg
super(APIException, self).__init__(msg, None)
class UnauthorizeError(APIException):
code = 401
msg = '权限错误!'
这里 有两种方式返回的方法,目前我理解的是
1.第一种 因为我已经做好了一个处理异常标记 所以 所有自定义异常都会经过那里被检测,统一格式返回
2.跳过了我错误检测 直接返回数据 其实HTTPException 也是继承了Exception只是对它封装了一些方法 它返回的数据是text的 你可以在get_headers中修改json返回格式
网友评论