Skip to content

Report a halted destroy as 422 instead of 200 - #131

Merged
dadachi merged 1 commit into
mainfrom
fix/destroy-failure-responses
Oct 4, 2026
Merged

dadachi merged 1 commit into
mainfrom
fix/destroy-failure-responses

Conversation

@dadachi

@dadachi dadachi commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Problem

The destroy actions (accounts, shops, item_tags, members, invitations, devices) ignored destroy's return value and always answered 200 or 204.

Nothing fails this way today:

But a before_destroy guard added later would leave the record in place behind a success response.

Fix

  • Use destroy! in all six actions.
  • rescue_from ActiveRecord::RecordNotDestroyed in Api::V1::Shopkeeper::BaseController. It answers 422 {code, error_message} with the record's errors, or "Could not be deleted." when there are none.

Verification

  • Test-first: test/integration/destroy_failure_test.rb adds a temporary before_destroy { throw :abort } to each model in turn, calls its endpoint, and expects 422 with the record kept. On the old code this got 200.
  • Red check:
    • Reverting each action to destroy fails the test at that action's own step.
    • Removing the rescue_from raises RecordNotDestroyed.
  • Dev server: normal deletes are unchanged; item tag and shop deletes both return {"status":200}. The probe records are gone.
  • bin/rails test: 498 runs, 0 failures. rubocop and brakeman are clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_01L4Pma9VAqB5A22KCEEin9R

The destroy actions ignored destroy's return value and always answered
success. No model halts its own destroy today, but a before_destroy
guard added later would have left the record in place behind a 200.
Use destroy! and rescue RecordNotDestroyed in the API base controller,
answering 422 with the record's errors (or "Could not be deleted.").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01L4Pma9VAqB5A22KCEEin9R
@dadachi
dadachi merged commit 1e9db81 into main Oct 4, 2026
3 checks passed
@dadachi
dadachi deleted the fix/destroy-failure-responses branch October 4, 2026 21:04
dadachi added a commit that referenced this pull request Oct 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant