Skip to content

chore: standardize error responses and add duplicate deck warning#82

Merged
TytaniumDev merged 1 commit intomainfrom
chore/error-responses-and-cleanup
Feb 27, 2026
Merged

chore: standardize error responses and add duplicate deck warning#82
TytaniumDev merged 1 commit intomainfrom
chore/error-responses-and-cleanup

Conversation

@TytaniumDev
Copy link
Owner

Summary

  • Add ApiErrorResponse and ApiUpdateResponse shared types in shared/types/api.ts
  • Create api/lib/api-response.ts with errorResponse, notFoundResponse, badRequestResponse helpers
  • Replace all ad-hoc error responses across 28 API route files with standard helpers
  • Remove unused REQUIRED_DECK_COUNT re-export from api/lib/types.ts
  • Add duplicate deck detection warning in POST /api/jobs response

Addresses architecture issues #4, #5, and #14 from docs/ARCHITECTURE_ISSUES.md.

Test plan

  • All API error responses use standard { error: string } shape
  • Sim PATCH idempotent responses unchanged ({ updated: false, reason })
  • Duplicate deck warning present in response when submitting identical deckIds
  • npm run lint --prefix api passes
  • npm run build --prefix api passes
  • npm run lint --prefix frontend passes
  • npm run build --prefix frontend passes
  • All existing unit tests still pass (217/217)

🤖 Generated with Claude Code

…#5, #14)

- Add `ApiErrorResponse` and `ApiUpdateResponse` shared types in shared/types/api.ts
- Create api/lib/api-response.ts with errorResponse, notFoundResponse, badRequestResponse helpers
- Replace all ad-hoc NextResponse.json({ error }) patterns across 28 API route files
  with standard helper functions for consistent error shape
- Remove unused REQUIRED_DECK_COUNT re-export from api/lib/types.ts
- Add duplicate deck detection warning in POST /api/jobs response

Intentionally left unchanged:
- Sim PATCH idempotent { updated: false, reason } responses (200s for Pub/Sub)
- Recover route { status: 'ok'/'terminal'/'not_found' } success responses
- Access request approve route (returns HTML)
- Auth helpers unauthorizedResponse/forbiddenResponse (use new Response with custom headers)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the API's robustness and user experience by standardizing error responses and introducing clearer type definitions for API interactions. It centralizes error handling logic, making the API more predictable and easier to consume, while also providing a helpful warning for users submitting jobs with potentially problematic duplicate decks.

Highlights

  • Standardized API Error Responses: Introduced a new utility file, api/lib/api-response.ts, which provides helper functions (errorResponse, notFoundResponse, badRequestResponse) to ensure consistent error response formats across the API. This refactors error handling in 28 API route files.
  • New API Response Types: Defined ApiErrorResponse and ApiUpdateResponse interfaces in shared/types/api.ts to standardize the structure of error and idempotent update responses, improving type safety and API consistency.
  • Duplicate Deck Warning: Added logic to the POST /api/jobs endpoint to detect if a job request includes duplicate deck IDs and returns a warning in the response, informing users that results may not be meaningful.
  • Code Cleanup: Removed an unused REQUIRED_DECK_COUNT re-export from api/lib/types.ts, streamlining the codebase.
Changelog
  • api/app/api/access-requests/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error responses with errorResponse calls for POST and GET endpoints.
  • api/app/api/admin/backfill-ratings/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error response with errorResponse call for the POST endpoint.
  • api/app/api/admin/pull-image/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error responses with errorResponse calls for unauthorized and broadcast failure scenarios.
  • api/app/api/deck-color-identity/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error response with errorResponse call for the GET endpoint.
  • api/app/api/decks/[id]/content/route.ts
    • Imported errorResponse, notFoundResponse, and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing ID, deck not found, and general errors.
  • api/app/api/decks/[id]/route.ts
    • Imported errorResponse, notFoundResponse, and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing ID, deck not found, permission errors, and general errors.
  • api/app/api/decks/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for listing decks, invalid URLs, missing parameters, and saving decks.
  • api/app/api/jobs/[id]/cancel/route.ts
    • Imported errorResponse, notFoundResponse, and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing ID, job not found, terminal job status, and general errors.
  • api/app/api/jobs/[id]/logs/condensed/route.ts
    • Imported errorResponse and notFoundResponse helpers.
    • Replaced ad-hoc error responses with helper calls for logs not found and general errors.
  • api/app/api/jobs/[id]/logs/raw/route.ts
    • Imported errorResponse and notFoundResponse helpers.
    • Replaced ad-hoc error responses with helper calls for logs not found and general errors.
  • api/app/api/jobs/[id]/logs/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for invalid game logs and general errors.
  • api/app/api/jobs/[id]/logs/simulation/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing filename/logText and general errors.
  • api/app/api/jobs/[id]/logs/structured/route.ts
    • Imported errorResponse and notFoundResponse helpers.
    • Replaced ad-hoc error responses with helper calls for logs not found and general errors.
  • api/app/api/jobs/[id]/recover/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for unauthorized requests, missing ID, and recovery failures.
  • api/app/api/jobs/[id]/route.ts
    • Imported errorResponse, notFoundResponse, and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing ID, job not found, invalid status transitions, and general errors across GET, DELETE, and PATCH methods.
  • api/app/api/jobs/[id]/simulations/[simId]/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing IDs, invalid states, and general update failures.
  • api/app/api/jobs/[id]/simulations/route.ts
    • Imported errorResponse, notFoundResponse, and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing ID, job not found, invalid count, and general errors.
  • api/app/api/jobs/[id]/stream/route.ts
    • Imported badRequestResponse, errorResponse, and notFoundResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing ID, GCP mode redirection, and job not found.
  • api/app/api/jobs/bulk-delete/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for invalid jobIds array and general errors.
  • api/app/api/jobs/next/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error responses with errorResponse calls for unauthorized requests and claiming next job failures.
  • api/app/api/jobs/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for listing jobs, rate limiting, invalid deck IDs, and job creation failures.
    • Added logic to detect duplicate deck IDs and include a warning in the POST response.
  • api/app/api/leaderboard/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error response with errorResponse call for fetching leaderboard failures.
  • api/app/api/sync/precons/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error responses with errorResponse calls for unauthorized requests and sync failures.
  • api/app/api/worker-setup/config/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing token, worker not configured, invalid token, invalid encryption key, and configuration read failures.
  • api/app/api/worker-setup/token/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error response with errorResponse call for worker setup not configured.
  • api/app/api/workers/[id]/route.ts
    • Imported errorResponse, notFoundResponse, and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for invalid maxConcurrentOverride, ownership checks, worker not found, and general update failures.
  • api/app/api/workers/heartbeat/route.ts
    • Imported errorResponse and badRequestResponse helpers.
    • Replaced ad-hoc error responses with helper calls for missing worker ID/name and heartbeat failures.
  • api/app/api/workers/route.ts
    • Imported errorResponse helper.
    • Replaced ad-hoc error response with errorResponse call for fetching workers failures.
  • api/lib/api-response.ts
    • Added new file defining errorResponse, notFoundResponse, and badRequestResponse helper functions for consistent API error handling.
  • api/lib/types.ts
    • Removed the unused export REQUIRED_DECK_COUNT.
  • shared/types/api.ts
    • Added new file defining ApiErrorResponse and ApiUpdateResponse interfaces for standardized API response shapes.
  • shared/types/index.ts
    • Exported ApiErrorResponse and ApiUpdateResponse types from the new api.ts file.
Activity
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

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

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 introduces a significant improvement by standardizing API error responses. The new helper functions (errorResponse, notFoundResponse, badRequestResponse) and shared types make the error handling more consistent, maintainable, and robust across the application. The addition of a warning for duplicate decks is also a valuable enhancement for users.

The refactoring has been applied systematically across numerous files, and the implementation is clean. The suggestion to further centralize error response generation by creating a helper for unauthorized responses is a valuable next step to complete the standardization effort, and should be addressed in this PR. Overall, this is a high-quality contribution.

export async function POST(req: NextRequest) {
if (!IS_LOCAL_MODE && !isWorkerRequest(req)) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
return errorResponse('Unauthorized', 401);

Choose a reason for hiding this comment

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

medium

While this correctly uses the new errorResponse helper, to further improve standardization, you could abstract this into a dedicated unauthorizedResponse helper within api-response.ts, similar to notFoundResponse and badRequestResponse.

This would centralize all common HTTP error responses. You could define it in api-response.ts as:

export function unauthorizedResponse(message: string = 'Unauthorized'): NextResponse<ApiErrorResponse> {
  return errorResponse(message, 401);
}

This would also allow for refactoring the existing unauthorizedResponse in lib/auth.ts to use this new helper, making all error responses flow through api-response.ts for maximum consistency.

References
  1. Address valid code quality suggestions immediately within the current pull request, even if it expands the scope. Do not defer them to a follow-up task.

@TytaniumDev TytaniumDev enabled auto-merge (squash) February 27, 2026 07:20
@TytaniumDev TytaniumDev merged commit 3d25488 into main Feb 27, 2026
6 checks passed
@TytaniumDev TytaniumDev deleted the chore/error-responses-and-cleanup branch February 27, 2026 07:21
TytaniumDev added a commit that referenced this pull request Feb 27, 2026
Resolve conflicts between Zod validation (PR #84) and error response
helpers (PR #82). Keep Zod schemas for validation, use badRequestResponse
helpers for error formatting, preserve duplicate deck detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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