Django exceptions reach DRF as 500

Đang mở
#6,391 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
django, python
Lĩnh vực
backend-api-design

Hướng nghiên cứu

Start with contentcuration/contentcuration/viewsets/base.py and the serialize_object behavior linked in the issue, then read the related DRF views and the exception handler strategy in #6387. Add regression tests for each input listed in Context and verify malformed detail-route pks return 404, invalid parameters and submission states return 400, and Django ObjectDoesNotExist returns 404.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

DEV: backend

Overview

Some code raises Django's ValidationError, ObjectDoesNotExist or FieldError inside DRF views. DRF converts only its own exceptions, Http404 and PermissionDenied, so these return 500. BaseValuesViewset.serialize_object also crashes on some malformed pks before it can return its 404.

Complexity: Medium
Target branch: hotfixes

Context

All inputs below were reproduced:

  • BaseValuesViewset.serialize_object:
    • On a model with an integer pk, GET /api/assessmentitem/abc raises UnboundLocalError, because .filter() fails before queryset is assigned. Kolibri fixed this in learningequality/kolibri@eeb4c8207e.
    • On SavedSearch, whose pk is a Django UUIDField, GET /api/search/saved-search/abc raises Django ValidationError, which isn't in the caught tuple.
    • Affected detail routes: assessmentitem, user, admin-users, bookmark, channeluser, communitylibrary_submission, admin_communitylibrary_submission, saved-search.
  • ChannelUserViewSet: GET /api/channeluser/<id> without ?channel= raises FieldError: Cannot resolve keyword 'can_edit'. can_edit and can_view exist only once the channel filter has annotated them.
  • ContentNodeTreeViewset.validate_and_return_params: depth=3, depth=abc, next__gt=-1 and next__gt=abc raise Django ValidationError on the anonymous public tree endpoint. Kolibri has the same code (learningequality/kolibri#15835).
  • resolve calls CommunityLibrarySubmission.save, which raises Django ValidationError when the author is no longer a channel editor or the channel has since become public.
The Change
  • serialize_object should follow Kolibri's fix and also treat Django ValidationError as not found.
  • channeluser retrieve without ?channel= should respond as the list route does. Each other site should return 400 instead of 500.
  • The custom EXCEPTION_HANDLER from the strategy in #6387 lands in this issue.
Acceptance Criteria
General
  • Each detail route listed in Context returns 404 for a malformed pk.
  • channeluser retrieve without ?channel= gets the same response as the list route without ?channel=.
  • Invalid depth and next__gt values return 400.
  • resolve returns 400 with the validation message for the two submission states in Context.
  • Django ValidationError raised in any DRF view returns 400, and ObjectDoesNotExist returns 404.
Testing
  • Each input in Context has a regression test.

AI usage

I scoped the audit and chose the validation strategy in #6387. Claude Code found and reproduced these failures and drafted this issue.

Ngôn ngữ chính
Python
Star
212
Fork
321
Merge trung bình
1 ngày 7 giờ
Pull request đã merge (30 ngày)
14

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của learningequality/studio

Tất cả issue của learningequality/studio

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.