-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: throw conflict error for duplicate roles/permissions #2845
Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@Flo4604 is attempting to deploy a commit to the Unkey Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughThis pull request standardizes error handling across the application by replacing inconsistent error codes. In multiple API endpoints, tests, and documentation, error codes such as "NOT_UNIQUE" and "PRECONDITION_FAILED" have been updated or replaced with "CONFLICT" to better represent duplicate entry errors, with corresponding changes to HTTP status codes (e.g., from 412 to 409) and error messages. Additionally, an obsolete key route has been removed, and a new OpenAPI error response for HTTP 412 has been introduced. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant API
participant Database
Client->>API: Sends request to create/update record
API->>Database: Attempt to insert/update entry
Database-->>API: Returns duplicate entry error
API->>API: Catches DatabaseError and maps error code to "CONFLICT" (409)
API->>Client: Sends error response with status 409 and error code "CONFLICT"
Assessment against linked issues
Possibly related PRs
Suggested labels
Suggested reviewers
📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Thank you for following the naming conventions for pull request titles! 🙏 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
apps/api/src/routes/v1_identities_updateIdentity.error.test.ts (1)
44-102
: Enhance test coverage for duplicate ratelimits error.While the status code assertion is correct, consider these improvements to make the test more robust:
- Add assertions for the error response body to verify the complete API contract.
- Make the test name more specific (e.g., "returns 409 CONFLICT for duplicate ratelimit names").
Apply this diff to enhance the test:
- test("duplicate ratelimits return an error", async (t) => { + test("returns 409 CONFLICT for duplicate ratelimit names", async (t) => { // ... existing test setup ... expect(res.status, `expected 409, received: ${JSON.stringify(res, null, 2)}`).toEqual(409); + expect(res.body).toMatchObject({ + error: { + code: "CONFLICT", + docs: "https://unkey.dev/docs/api-reference/errors/code/CONFLICT", + message: "Ratelimit names must be unique", + }, + }); });
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
apps/api/src/routes/v1_identities_createIdentity.error.test.ts
(1 hunks)apps/api/src/routes/v1_identities_updateIdentity.error.test.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- apps/api/src/routes/v1_identities_createIdentity.error.test.ts
apps/api/src/routes/v1_permissions_createPermission.error.test.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
apps/api/src/routes/v1_permissions_createRole.error.test.ts (1)
41-75
: LGTM! Well-structured test for duplicate role handling.The test effectively validates the new conflict error handling for duplicate roles. Good use of typed requests and detailed error messages in assertions.
Consider adding assertions for the error response body to verify the correct error code and message. Example:
expect( errorResponse.status, `expected 409, received: ${JSON.stringify(errorResponse, null, 2)}`, ).toBe(409); +expect(errorResponse.body).toMatchObject({ + error: { + code: "CONFLICT", + message: expect.stringContaining("duplicate"), + docs: expect.stringContaining("unkey.dev/docs/api-reference/errors") + } +});
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
apps/api/src/routes/v1_identities_createIdentity.error.test.ts
(1 hunks)apps/api/src/routes/v1_identities_updateIdentity.error.test.ts
(3 hunks)apps/api/src/routes/v1_permissions_createPermission.error.test.ts
(2 hunks)apps/api/src/routes/v1_permissions_createRole.error.test.ts
(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
- apps/api/src/routes/v1_identities_createIdentity.error.test.ts
- apps/api/src/routes/v1_identities_updateIdentity.error.test.ts
- apps/api/src/routes/v1_permissions_createPermission.error.test.ts
🔇 Additional comments (1)
apps/api/src/routes/v1_permissions_createRole.error.test.ts (1)
3-3
: LGTM! Good choice using node:crypto for UUID generation.Using the native
randomUUID
function is a good practice for generating unique test data.
What does this PR do?
PRECONDITION_FAILED
, which pannes in the createIdentity endpoint.apps/api/src/routes/v1_keys_ami.ts
which was not being used as its a duplicate ofapps/api/src/routes/v1_keys_whoami.ts
PRECONDITION_FAILED
PRECONDITION_FAILED
toCONFLICT
Type of change
How should this be tested?
Running the createPermission and createRole tests.
Checklist
Required
pnpm build
pnpm fmt
console.logs
git pull origin main
Appreciated
Summary by CodeRabbit