Skip to content

api: return 400 instead of 500 on duplicate changefeed creation#5428

Open
rajneesh2k10 wants to merge 1 commit into
pingcap:masterfrom
rajneesh2k10:fix/5427-duplicate-changefeed-status
Open

api: return 400 instead of 500 on duplicate changefeed creation#5428
rajneesh2k10 wants to merge 1 commit into
pingcap:masterfrom
rajneesh2k10:fix/5427-duplicate-changefeed-status

Conversation

@rajneesh2k10

@rajneesh2k10 rajneesh2k10 commented Jun 16, 2026

Copy link
Copy Markdown

ErrChangeFeedAlreadyExists was not in the httpBadRequestError registry, so the v2 CreateChangefeed handler returned HTTP 500 when a client posted a changefeed ID that already existed. A duplicate-create is a client-side condition and should be a 4xx.

Adds the error to the existing bad-request registry, mirroring how ErrChangeFeedNotExists is already classified, and adds a TestErrorHandleMiddleware regression test.

close #5427

What problem does this PR solve?

Issue Number: close #5427

The v2 POST /api/v2/changefeeds endpoint returned HTTP 500 when the requested changefeed ID already existed, because
ErrChangeFeedAlreadyExists was not registered in the middleware's bad-request error list. Clients could not distinguish
this duplicate condition from a real server fault.

What is changed and how it works?

  • pkg/api/util.go: add ErrChangeFeedAlreadyExists to httpBadRequestError, grouped next to ErrChangeFeedNotExists. The
    existing IsHTTPBadRequestError matcher already handles direct equality, RFC code, and wrapped errors, so no other plumbing
    is needed.
  • api/middleware/middleware_test.go: add TestErrorHandleMiddleware pinning the 200 / 400 / 500 buckets, including the
    duplicate-changefeed → 400 case as a regression guard.

Check List

Tests

  • Unit test

Questions

Will it cause performance regression or break compatibility?

Changes the HTTP response code for one error case from 500 to 400. Clients that previously treated the 500 as a transient
server fault and retried will now see a 4xx and (correctly) stop retrying. No wire-format or API-shape changes.

Do you need to update user documentation, design documentation or monitoring documentation?

No.

Release note

Fix `POST /api/v2/changefeeds` returning HTTP 500 instead of 400 when the changefeed ID already exists.

Summary by CodeRabbit

  • Bug Fixes
    • Improved API error handling to return appropriate HTTP 400 status codes for invalid requests, including scenarios involving duplicate changefeeds.

ErrChangeFeedAlreadyExists was not in the httpBadRequestError
registry, so the v2 CreateChangefeed handler returned HTTP 500
when a client posted a changefeed ID that already existed. A
duplicate-create is a client-side condition and should be a 4xx.

Adds the error to the existing bad-request registry, mirroring
how ErrChangeFeedNotExists is already classified, and adds a
TestErrorHandleMiddleware regression test.

close pingcap#5427
@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-linked-issue release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Jun 16, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign charlescheung96 for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot Bot added contribution This PR is from a community contributor. first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Jun 16, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

Hi @rajneesh2k10. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@ti-chi-bot

ti-chi-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

Welcome @rajneesh2k10!

It looks like this is your first PR to pingcap/ticdc 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to pingcap/ticdc. 😃

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ce1f5744-8fdc-4362-ba8a-05585409dfbf

📥 Commits

Reviewing files that changed from the base of the PR and between 50cfdb0 and 450b5e7.

📒 Files selected for processing (2)
  • api/middleware/middleware_test.go
  • pkg/api/util.go

📝 Walkthrough

Walkthrough

pkg/api/util.go adds cerror.ErrChangeFeedAlreadyExists to the httpBadRequestError list so IsHTTPBadRequestError now returns true for duplicate changefeed errors. A new test TestErrorHandleMiddleware in api/middleware/middleware_test.go validates the full HTTP status code mapping of ErrorHandleMiddleware.

Changes

ErrChangeFeedAlreadyExists HTTP 400 fix and test

Layer / File(s) Summary
Bad-request classification fix and middleware test
pkg/api/util.go, api/middleware/middleware_test.go
httpBadRequestError gains cerror.ErrChangeFeedAlreadyExists, making IsHTTPBadRequestError return true for duplicate changefeed errors. TestErrorHandleMiddleware covers 400 for invalid param and duplicate changefeed, 200 for no error, and 500 for unclassified errors.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Suggested labels

size/XS

Suggested reviewers

  • tenfyzhong
  • asddongmen

Poem

🐰 A changefeed twice? That's quite a blunder,
No more five-hundreds shall tear asunder!
Four hundred now greets the duplicate call,
A tiny fix, but it fixes it all.
Hop hop, the rabbit checks the test — hooray! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: correcting HTTP response code from 500 to 400 for duplicate changefeed creation, which aligns with the primary objective of the PR.
Description check ✅ Passed The description comprehensively covers the problem statement, solution details, testing approach, compatibility considerations, and includes a release note. All required sections from the template are addressed.
Linked Issues check ✅ Passed The PR fully addresses issue #5427 by registering ErrChangeFeedAlreadyExists in the bad-request error list and adding a regression test that validates the 400 response for duplicate changefeeds.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the HTTP 500 to 400 response code issue: adding the error to the registry and testing the fix. No unrelated modifications were introduced.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@ti-chi-bot ti-chi-bot Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jun 16, 2026
@pingcap-cla-assistant

pingcap-cla-assistant Bot commented Jun 16, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds cerror.ErrChangeFeedAlreadyExists to the list of errors mapped to an HTTP 400 Bad Request status in pkg/api/util.go. It also introduces a new unit test TestErrorHandleMiddleware in api/middleware/middleware_test.go to verify the error handling middleware's behavior under different scenarios. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@ti-chi-bot

ti-chi-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

[FORMAT CHECKER NOTIFICATION]

Notice: To remove the do-not-merge/needs-linked-issue label, please provide the linked issue number on one line in the PR body, for example: Issue Number: close #123 or Issue Number: ref #456.

📖 For more info, you can check the "Contribute Code" section in the development guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution This PR is from a community contributor. do-not-merge/needs-linked-issue first-time-contributor Indicates that the PR was contributed by an external member and is a first-time contributor. needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cdc/api: ErrChangeFeedAlreadyExists returns HTTP 500 instead of 400

1 participant