色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Django REST framework 異常處理

瀏覽:8日期:2024-09-03 16:58:22
目錄寫在前面DRF異常處理1. DRF 常見的異常2. 自定義異常3. 使用自定義異常4. 驗(yàn)證結(jié)果異常處理進(jìn)階1. 修改自定義異常2. 自定義更多異常3. 新增測(cè)試接口4. 驗(yàn)證結(jié)果總結(jié)參考資料寫在前面

這兩天一直在思索關(guān)于 DRF 還有哪些是項(xiàng)目必備的而且還沒有說到的基礎(chǔ)性的知識(shí)。這不昨天寫到日志相關(guān)的功能就直接想到還有異常處理相關(guān)的功能,其實(shí)在之前項(xiàng)目中初期是沒有統(tǒng)一的異常捕獲手段。可能是 DRF 自帶的異常 能滿足大多數(shù)功能,也可能是比較懶,就使用比較粗暴的方式,以狀態(tài)碼 500 的方式去拋出異常,然后在日志中可以看到所有的異常信息。這么做呢,代碼其實(shí)是不夠健壯的,前端在調(diào)用的時(shí)候莫名的 500 也是不夠友好的,所以今天就補(bǔ)充一下異常相關(guān)的知識(shí)。

DRF異常處理1. DRF 常見的異常 AuthenticationFailed/ NotAuthenticated 一般該異常狀態(tài)碼為'401 Unauthenticated',主要是沒有登錄鑒權(quán)的時(shí)候會(huì)返回,可以用在自定義登錄的時(shí)候。 PermissionDenied 一般用在鑒權(quán)時(shí)候使用,一般狀態(tài)碼為'403 Forbidden'。 ValidationError 一般狀態(tài)碼為'400 Bad Request',主要是 serializers 中對(duì)字段的校驗(yàn),比如對(duì)字段類型的校驗(yàn)、字段長度的校驗(yàn)以及自定義字段格式的校驗(yàn)。2. 自定義異常

這里對(duì)異常的定義主要的想法來自 ValidationError,統(tǒng)一異常返回的格式,方便前端統(tǒng)一處理類似異常。

自定義異常

# 新建 utils/custom_exception.pyclass CustomException(Exception): _default_code = 400 def __init__(self,message: str = '',status_code=status.HTTP_400_BAD_REQUEST,data=None,code: int = _default_code, ):self.code = codeself.status = status_codeself.message = messageif data is None: self.data = {'detail': message}else: self.data = data def __str__(self):return self.message

自定義異常處理

# utils/custom_exception.pyfrom rest_framework.views import exception_handlerdef custom_exception_handler(exc, context): # Call REST framework’s default exception handler first, # to get the standard error response.# 這里對(duì)自定義的 CustomException 直接返回,保證系統(tǒng)其他異常不受影響 if isinstance(exc, CustomException):return Response(data=exc.data, status=exc.status) response = exception_handler(exc, context) return response

配置自定義異常處理類

REST_FRAMEWORK = { # ... 'EXCEPTION_HANDLER': 'utils.custom_exception.custom_exception_handler',}3. 使用自定義異常

使用之前文章的接口用來測(cè)試自定義異常的處理

class ArticleViewSet(viewsets.ModelViewSet): ''' 允許用戶查看或編輯的API路徑。 ''' queryset = Article.objects.all() serializer_class = ArticleSerializer @action(detail=False, methods=['get'], url_name='exception', url_path='exception') def exception(self, request, *args, **kwargs):# 日志使用 demologger.error('自定義異常')raise CustomException(data={'detail': '自定義異常'})4. 驗(yàn)證結(jié)果

$ curl -H ’Accept: application/json; indent=4’ -u admin:admin http://127.0.0.1:8000/api/article/exception/{ 'detail': '自定義異常'}異常處理進(jìn)階

上面的代碼雖說是可以滿足90%的需求,但是錯(cuò)誤的定義太泛泛。難以集中定義管理錯(cuò)誤,與常見項(xiàng)目中自定義的異常比較優(yōu)點(diǎn)就是靈活,但是隨著代碼中拋出的異常越來越多加之散落在各個(gè)角落,不利于更新維護(hù)。所以下面對(duì)修改一下代碼,對(duì)異常有統(tǒng)一的定義,同時(shí)也支持自定義返回HTTP狀態(tài)碼。

1. 修改自定義異常

# utils/custom_exception.pyclass CustomException(Exception): # 自定義code default_code = 400 # 自定義 message default_message = None def __init__( self, status_code=status.HTTP_400_BAD_REQUEST, code: int = None, message: str = None, data=None, ):self.status = status_codeself.code = self.default_code if code is None else codeself.message = self.default_message if message is None else messageif data is None: self.data = {'detail': self.message, 'code': self.code}else: self.data = data def __str__(self):return str(self.code) + self.message2. 自定義更多異常

class ExecuteError(CustomException): '''執(zhí)行出錯(cuò)''' default_code = 500 default_message = '執(zhí)行出錯(cuò)'class UnKnowError(CustomException): '''執(zhí)行出錯(cuò)''' default_code = 500 default_message = '未知出錯(cuò)'3. 新增測(cè)試接口

class ArticleViewSet(viewsets.ModelViewSet): ''' 允許用戶查看或編輯的API路徑。 ''' queryset = Article.objects.all() serializer_class = ArticleSerializer @action(detail=False, methods=['get'], url_name='exception', url_path='exception') def exception(self, request, *args, **kwargs):# 日志使用 demologger.error('自定義異常')raise CustomException(data={'detail': '自定義異常'}) @action(detail=False, methods=['get'], url_name='unknown', url_path='unknown') def unknown(self, request, *args, **kwargs):# 日志使用 demologger.error('未知錯(cuò)誤')raise UnknownError() @action(detail=False, methods=['get'], url_name='execute', url_path='execute') def execute(self, request, *args, **kwargs):# 日志使用 demologger.error('執(zhí)行錯(cuò)誤')raise ExecuteError()4. 驗(yàn)證結(jié)果

curl -H ’Accept: application/json; indent=4’ -u admin:admin http://127.0.0.1:8000/api/article/unknown/{ 'detail': '未知出錯(cuò)', 'code': 500}$ curl -H ’Accept: application/json; indent=4’ -u admin:admin http://127.0.0.1:8000/api/article/execute/{ 'detail': '執(zhí)行出錯(cuò)', 'code': 500}總結(jié)

需要注意自定義的異常處理函數(shù)需要在處理完成自定義異常后繼續(xù)執(zhí)行 rest_framework.views.exception_handler,因?yàn)檫@里的執(zhí)行仍然需要兼容已有的異常處理;下面貼一下 DRF 有關(guān)的異常處理邏輯。

該處理函數(shù)默認(rèn)處理 APIException以及 Django 內(nèi)部的 Http404 PermissionDenied,其他的異常會(huì)返回 None ,會(huì)觸發(fā) DRF 500 的錯(cuò)誤。

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, Http404):exc = exceptions.NotFound() elif isinstance(exc, PermissionDenied):exc = exceptions.PermissionDenied() if isinstance(exc, exceptions.APIException):headers = {}if getattr(exc, ’auth_header’, None): headers[’WWW-Authenticate’] = exc.auth_headerif getattr(exc, ’wait’, None): headers[’Retry-After’] = ’%d’ % exc.waitif isinstance(exc.detail, (list, dict)): data = exc.detailelse: data = {’detail’: exc.detail}set_rollback()return Response(data, status=exc.status_code, headers=headers) return None參考資料

Django REST framework 異常文檔Django 異常文檔

到此這篇關(guān)于Django REST framework 異常處理的文章就介紹到這了,更多相關(guān)Django REST framework 異常內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 欧美一区二区高清 | 99re66热这里只有精品免费观看 | 福利一二三区 | 久草在线在线 | 亚洲一区在线视频 | 亚洲一区二区三区在线视频 | 日韩中文字幕免费在线观看 | 国产精品毛片va一区二区三区 | 亚洲国产日韩女人aaaaaa毛片在线 | 欧美亚洲一区二区三区四 | 久久精品国产一区二区三区 | 欧美一区二区三区久久综合 | 黑人一级片 | 天天舔夜夜操 | 国产精品二 | 高清国产精品久久久久 | 男人天堂怡红院 | 免费 欧美 自拍 在线观看 | 久久精品视频2 | 亚洲国产成人综合精品2020 | 88av在线视频 | 国产视频久久久久 | 欧美另类videosbestsex视频 | 欧美老头老太做爰xxxx | 99在线精品免费视频九九视 | 97久久免费视频 | 国产99视频精品草莓免视看 | 艳女伦交一级毛片 | 国产一区中文字幕在线观看 | 免费一级a毛片在线播 | 黄色a∨ | 啪视| 国产高清在线 | 欧美片a| 久草免费手机视频 | 亚洲永久中文字幕在线 | 国产在线观看免费视频软件 | 华人黄网站 | 国产精品一区二区久久精品涩爱 | 亚洲精品国产美女在线观看 | 成人软件网18免费视频 |