def _handler500(request=None, exception=None): exception = sys.exc_info() response = exception_process(exception=exception, context=None) res = get_dataformat(request) response.data['status_code'] = _HANDLER500_CODE return HttpResponseServerError(json.dumps(response.data), content_type=res.content_type)
def _handler403(request=None, exception=None): """重写django系统对400、403、404、500code报错的机制handler""" response = exception_handler(exc=exception, context=None) res = get_dataformat(request) response.data['debuginfo'] = repr(exception) log.error(repr(exception)) response.data['status_code'] = _HANDLER403_CODE return HttpResponseForbidden(json.dumps(response.data), content_type=res.content_type)
def _handler500(request=None, exception=None): """重写django系统对400、403、404、500code报错的机制handler""" exception = sys.exc_info() response = exception_process(exception=exception, context=None) res = get_dataformat(request) response.data['status_code'] = _HANDLER500_CODE print(res) return HttpResponseServerError(json.dumps(response.data), content_type=res.content_type)
def _handler404(request=None, exception=None): response = exception_handler(exc=exception, context=None) res = get_dataformat(request) args = exception.args for _ in args: # 404报错会在tried中返回所有的路由,隐私关系我们只返回'all_modules' if _.get('tried'): _['tried'] = 'all_modules' response.data['debuginfo'] = repr(exception) log.error(repr(exception)) response.data['status_code'] = _HANDLER404_CODE return HttpResponseNotFound(json.dumps(response.data), content_type=res.content_type)
def inner(request, *args, **kwargs): # methods校验 try: assert request.method in methods except AssertionError: dataformat = get_dataformat(request) message = 'Method Not Allowed ({method}): {path}'.format( method=request.method, path=request.path) r = dict(status_code=HTTP_405_METHOD_NOT_ALLOWED, detail=message) response = HttpResponseNotAllowed( methods, json.dumps(r), content_type=dataformat.content_type) log.warn(message) return response # request.user校验 try: assert login_required check_user(request.user, perm) except AssertionError: pass except InsufficientPermissionsError: message = 'user get no permission (perm:{perm})'.format( perm=perm) log.warn(message) raise InsufficientPermissionsError(detail=message) # NOTE:推荐 # request.jwt校验 try: assert jwt_required jwt_token_verify_handler(request.jwt) except AssertionError: pass except: message = 'user not authentication' log.warn(message) raise InvalidJwtToken(detail=message) return func(request, *args, **kwargs)