restframework內置了基本的異常處理,然而並不夠用前端
Django REST framework的各類技巧【目錄索引】ajax
異常時爲了保持api的一致,應該返回jsondjango
error_codejson
給開發看的error_messagesegmentfault
給用戶看的messageapi
源碼中的異常處理,可見是不符合需求的。服務器
def exception_handler(exc, context): """ Returns the response that should be used for any given exception. By default we handle the REST framework `APIException`, and also Django's built-in `Http404` and `PermissionDenied` exceptions. Any unhandled exceptions may return `None`, which will cause a 500 error to be raised. """ if isinstance(exc, exceptions.APIException): headers = {} if getattr(exc, 'auth_header', None): headers['WWW-Authenticate'] = exc.auth_header if getattr(exc, 'wait', None): headers['Retry-After'] = '%d' % exc.wait if isinstance(exc.detail, (list, dict)): data = exc.detail else: data = {'detail': exc.detail} set_rollback() return Response(data, status=exc.status_code, headers=headers) elif isinstance(exc, Http404): msg = _('Not found.') data = {'detail': six.text_type(msg)} set_rollback() return Response(data, status=status.HTTP_404_NOT_FOUND) elif isinstance(exc, PermissionDenied): msg = _('Permission denied.') data = {'detail': six.text_type(msg)} set_rollback() return Response(data, status=status.HTTP_403_FORBIDDEN) # Note: Unhandled exceptions will raise a 500 error. return None
注意我在handler最後一行註釋掉的代碼,必定不能抓最後的異常,這樣log的信息會沒有各類堆棧,很是不全ui
class Error(Exception): def __init__(self, err_code, err_message='Internal Server Error', message=u'服務器異常', status_code=status.HTTP_400_BAD_REQUEST): self.err_code = err_code self.err_message = err_message self.message = message self.status_code = status_code def __unicode__(self): return u'[Error] %d: %s(%d)' % (self.err_code, self.err_message, self.status_code) def getResponse(self): return ErrorResponse(self.err_code, self.err_message, self.message, self.status_code) def ErrorResponse(err_code=errors.SYSTEM_ERROR, err_message='Internal Server Error', message=u'服務器異常', status=status.HTTP_400_BAD_REQUEST, headers=None): err = { 'error_code': err_code, 'error': err_message, 'message': message, } return Response(err, status, headers=headers) def custom_exception_handler(exc, context): if isinstance(exc, Error): set_rollback() return ErrorResponse(exc.err_code, exc.err_message, exc.message, status=exc.status_code) if isinstance(exc, (ForeignObjectRelDeleteError, ModelDontHaveIsActiveFiled)): set_rollback() return ErrorResponse(errors.PermissionDenied, unicode(exc), u'抱歉, 已有其餘數據與之關聯, 禁止刪除', status=status.HTTP_403_FORBIDDEN) if isinstance(exc, (RestPermissionDenied, PermissionDenied)): msg = _('Permission denied.') data = { 'detail': six.text_type(msg) } exc_message = str(exc) if 'CSRF' in exc_message: data['detail'] = exc_message set_rollback() return ErrorResponse(errors.PermissionDenied, data, u'opps, 您沒有對應的權限', status=status.HTTP_403_FORBIDDEN) .... log.error(exc) # Note: Unhandled exceptions will raise a 500 error. #return ErrorResponse(errors.SYSTEM_ERROR, 'Internal Server Error', status.HTTP_500_INTERNAL_SERVER_ERROR)
而後在settings.py中加上 'EXCEPTION_HANDLER': 'api.exception_handler.custom_exception_handler',
url
經過這樣的實現你就能夠直接在代碼中丟異常了,而前端收到的是一個http code爲400的json responserest
@POST('name', validators='required') def create(self, request, name, *args, **kwargs): try: Term.objects.get(name=name, is_active=True) raise Error(errors.Exist, err_message=u'已存在對應的年份', message=u'已存在對應的年份') except Term.DoesNotExist: pass return super(TermsView, self).create(request, *args, **kwargs)
使用django的自定義404,500handler的方式解決
url.py裏面加上 handler500 = 'service.views.render_500'
def render_500(request): if request.is_ajax(): err = { 'error_code': errors.SYSTEM_ERROR, 'error': 'Internal Server Error', 'message': 'Internal Server Error', } return JsonResponse(err, status=status.HTTP_500_INTERNAL_SERVER_ERROR) return redirect('/error/?c=500')