Skip to content

Return 404 instead of 500 when updating a couch document that does not exist - #38047

Draft
gherceg wants to merge 5 commits into
gh/fix-create-on-update-bugfrom
gh/fix-couch-resource-404s
Draft

Return 404 instead of 500 when updating a couch document that does not exist#38047
gherceg wants to merge 5 commits into
gh/fix-create-on-update-bugfrom
gh/fix-couch-resource-404s

Conversation

@gherceg

@gherceg gherceg commented Aug 20, 2026

Copy link
Copy Markdown
Member

Product Description

PUT to a mobile worker or group ID that does not exist now returns 404 instead of 500. GET for an unknown UCR data source likewise returns 404 instead of 500. (UCR's PUT was already fixed by #38045, this PR fixes its GET.)

Technical Summary

https://dimagi.atlassian.net/browse/SAAS-20201

#38045 is what stops tastypie creating a new object when obj_update raises NotFound. CommCareUserResource and GroupResource both define obj_create, so if this PR landed on its own, a PUT to a missing ID would silently create a new mobile worker or group instead of returning 500.

CouchResourceMixin.get_document_for_update now does the lookup and converts ObjectDoesNotExist into NotFound, reading the document class off _meta.object_class so callers pass only ID and domain.

The downside of this change is that it still relies on those creating APIs on couch resources to use this helper in the obj_update method, but ideally we aren't adding new APIs backed by couch models.

Feature Flag

N/A — the UCR data source endpoints sit behind the pre-existing USER_CONFIGURABLE_REPORTS flag.

Safety Assurance

Safety story

Every affected request previously returned a 500.

Automated test coverage

Full corehq/apps/api/tests/ suite passes.

QA Plan

No QA needed.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations

Labels & Review

  • Risk label is set correctly
  • The set of people pinged as reviewers is appropriate for the level of risk of the change

@dimagimon dimagimon added the Risk: High Change affects files that have been flagged as high risk. label Aug 20, 2026
@gherceg
gherceg force-pushed the gh/fix-create-on-update-bug branch from 8c91c39 to d30c334 Compare August 20, 2026 14:38
Ensures that a 404 is returned when a couch resource that uses this
helper is not found
@gherceg
gherceg force-pushed the gh/fix-couch-resource-404s branch from f94cc81 to 4465fdf Compare August 20, 2026 14:46
gherceg and others added 2 commits August 20, 2026 09:58
obj_get raised tastypie's NotFound, which get_detail does not catch, so a
GET for an unknown id returned a 500 rather than a 404.

The obj_update change is a refactor with no behavior change: put_detail
already answered 404 once it stopped echoing the exception message. It
replaces the duplicated Http404 conversion with the couch mixin helper.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@gherceg
gherceg force-pushed the gh/fix-couch-resource-404s branch from 4465fdf to ffd215a Compare August 20, 2026 14:59
@gherceg gherceg added product/invisible Change has no end-user visible impact and removed DON'T REVIEW YET labels Aug 20, 2026
obj_get raised tastypie's NotFound, which get_detail does not catch, so a
GET for an unknown or cross-domain id returned a 500. Same fix already
applied to the data source resource in the previous commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

product/invisible Change has no end-user visible impact Risk: High Change affects files that have been flagged as high risk.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants