赞
踩
官网: https://www.django-rest-framework.org/api-guide/exceptions/
REST framework定义的异常
也就是说,很多的没有在上面列出来的异常,就需要我们在自定义异常中自己处理了。
官网: https://www.django-rest-framework.org/api-guide/exceptions/
APIException是所有在 APIView class 或 @api_view.异常的基类,
若要自定义异常,请继承 APIException 并在该类上设置. status _ code、 . default _ detail 和 default _ code 属性。
例如,如果您的 API 依赖于有时可能无法访问的第三方服务,您可能希望实现“503服务不可用”HTTP 响应代码的异常。你可以这样做:
from rest_framework.exceptions import APIException
class ServiceUnavailable(APIException):
status_code = 503
default_detail = 'Service temporarily unavailable, try again later.'
default_code = 'service_unavailable'
django REST 自定义返回格式
参考URL: https://blog.csdn.net/a54288447/article/details/126154717
继承官方的Response类
Django-drf-全局异常处理
参考URL: https://zhuanlan.zhihu.com/p/288814772
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。