-
Notifications
You must be signed in to change notification settings - Fork 0
/
middlewares.py
33 lines (27 loc) · 928 Bytes
/
middlewares.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# Create by Annhuny On 2019-12-29 22:34
# File Name : middlewares.py
import json
import logging
import sys
import traceback
from django.http import JsonResponse
from django.utils.deprecation import MiddlewareMixin
from DB.sqlalchemy_session import session
from errors import CustomError, DEFAULT_ERR_CODE
logger = logging.getLogger('django.request')
class SessionClose(MiddlewareMixin):
def process_response(self, request, response):
session.close()
return response
class ExceptionHandler(MiddlewareMixin):
def process_exception(self, request, e):
if isinstance(e, CustomError):
res = {'code': e.code, 'msg': e.msg}
return JsonResponse(res)
else:
logger.warning(traceback.format_exc())
return JsonResponse({
'code': DEFAULT_ERR_CODE,
'msg': traceback.format_exc(),
})