- 分享一个前后端分离(Python3 + Flask +Vuew)的个人博客,一个使用Python Flask 框架编写的个人博客。项目很轻。却很实用。
- GitHub地址:哆啦瞎梦
- 技术栈:Flask、Vue、Docker
- 初始化,项目结构我就不再写了,之前写过了,没有的可以看我之前的文章。或者直接私信我,我特地准备了一个干净的模板
flask-restful 不做果断解释
# 在flask_app>restful_api_manage中创建一个base.py
from flask import request
from flask_restful import Resource
# 同一错误集中处理
from common.error import AppError, NotFoundError, UnauthorizedError, ValidationError, ParaValidateFailError, \
MoreValidationError, UnauthorizeError
# 统一方法返回的
from flask_app.restful_api_manage.hepler import error_response_default, unauthorized_response
class BaseErrorHandler(Resource):
"""
注册错误管理机制
get/post/delete/patch/put/head
"""
def base_error_check(handler, handler_func):
def _check(*args, **kwargs):
added_error_instances = (
AppError, NotFoundError, UnauthorizedError, ValidationError, ParaValidateFailError, MoreValidationError)
try:
ret = handler_func(*args, **kwargs)
except added_error_instances as e:
code = getattr(e, "code", 500)
code = 500 if code < 200 else code
return error_response_default(handler, code, e.msg, e.error_code)
else:
return ret
return _check
def __init__(self):
if hasattr(self, 'get'):
self.get = base_error_check(self, self.get)
if hasattr(self, 'post'):
self.post = base_error_check(self, self.post)
if hasattr(self, 'delete'
self.delete = base_error_check(self, self.delete)
if hasattr(self, 'patch'):
self.patch = base_error_check(self, self.patch)
if hasattr(self, 'put'):
self.put = base_error_check(self, self.put)
if hasattr(self, 'head'):
self.head = base_error_check(self, self.head)
class BaseModelHandler(BaseErrorHandler):
def __init__(self):
super(BaseModelleHandler, self).__init__()
- 这一块主要是对Resource封装了一个方法 捕捉错误异常格式化返回数据
网友评论