Skip to content

refactor: implement type-safe HTTP helpers and response patterns#128

Merged
JDIZM merged 2 commits intomainfrom
refactor-response-error-handling
Oct 22, 2025
Merged

refactor: implement type-safe HTTP helpers and response patterns#128
JDIZM merged 2 commits intomainfrom
refactor-response-error-handling

Conversation

@JDIZM
Copy link
Owner

@JDIZM JDIZM commented Oct 16, 2025

Summary

Refactor HTTP error handling and API response patterns to use type-safe helpers across the application.

Changes

Core HTTP Helpers

  • Replace HttpError.ts with Http.ts for better organization
  • Add HttpStatusCode enum for type-safe status codes
  • Add ErrorCode enum for consistent error codes
  • Implement HttpErrors factory with predefined error types (NotFound, ValidationFailed, Unauthorized, etc.)
  • Update apiResponse helper with integrated logging and type safety
  • Add AccountStatus enum for type-safe account status checks

Handler & Middleware Migration

  • Migrate 30+ handlers across accounts, admin, auth, memberships, workspaces, and audit logs
  • Update all middleware (isAuthenticated, isAuthorized, checkAccountStatus, errorHandler, rateLimiter)
  • Replace handleHttpError with direct apiResponse.error usage
  • Implement proper 404 responses for missing resources
  • Update getAccountWithRelations to return null for missing accounts

Input Validation

  • Add Zod validation schema for audit logs query parameters
  • Validate UUIDs, dates, pagination params with proper coercion
  • Return structured validation errors with detailed messages

Impact

  • Consistent error responses with proper HTTP status codes throughout the API
  • Type safety enforced at compile time for status codes and error types
  • Integrated logging within apiResponse helper (removed redundant logger calls)
  • Better error handling with null returns instead of thrown errors
  • Input validation prevents invalid query parameters

@gemini-code-assist
Copy link

Summary of Changes

Hello @JDIZM, 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 refactors the application's HTTP error handling and API response generation. The core change involves introducing a new Http.ts module that centralizes HTTP status codes, custom error codes, and a robust HttpError class. This new foundation is then integrated into a redesigned apiResponse helper, which now provides type-safe methods for both success and error responses, including built-in logging. This standardization ensures a more predictable, maintainable, and type-safe approach to API communication across the entire codebase, improving developer experience and reducing potential errors.

Highlights

  • Centralized HTTP Helpers: Introduced src/helpers/Http.ts to consolidate HttpStatusCode enum, ErrorCode enum, a custom HttpError class, and an HttpErrors factory for consistent error creation.
  • Type-Safe API Responses: The src/helpers/response.ts module was refactored into a new apiResponse helper, integrating the new HttpError and HttpStatusCode for enhanced type safety and a standardized response format.
  • Streamlined Error Handling: Replaced the handleHttpError function with direct usage of apiResponse.error, simplifying error propagation and ensuring consistent error messages and codes.
  • Integrated Logging: Logging for both success and error responses is now handled directly within the apiResponse helper, removing redundant logger.info calls from individual handlers and middleware.
  • Comprehensive Migration: All relevant API handlers (accounts, admin, auth, me, memberships, workspaces) and middleware (checkAccountStatus, errorHandler, isAuthenticated, isAuthorized, rateLimiter) have been updated to utilize the new type-safe HTTP helpers and response patterns.
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.

- Replace HttpError.ts with Http.ts for better organization
- Add HttpStatusCode enum for type-safe status codes
- Add ErrorCode enum for consistent error codes
- Implement HttpErrors factory with predefined error types
- Update apiResponse helper with logging and type safety
- Migrate all handlers and middleware to new pattern
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 does a great job of refactoring the HTTP error handling and response patterns across the application. The introduction of Http.ts with HttpStatusCode, ErrorCode, and the HttpErrors factory provides a much-needed type-safe and consistent way to handle API responses. The new apiResponse helper is also a significant improvement, centralizing logging and response formatting. The migration of all handlers and middleware to this new pattern is thorough.

I've left a few comments on specific areas for improvement, mainly concerning a case of incorrect error handling, a removed TODO that should be addressed, and a couple of suggestions to further improve code quality and maintainability. Overall, this is a solid and valuable refactoring.

@JDIZM JDIZM force-pushed the refactor-response-error-handling branch from 4880487 to 3c91d45 Compare October 16, 2025 15:18
- Fix account not found to return 404 instead of 400
- Update getAccountWithRelations to return null instead of throwing
- Add AccountStatus enum to replace magic strings
- Add Zod validation for audit logs query parameters
- Improve type safety across account and audit log handlers
@JDIZM JDIZM merged commit 6380406 into main Oct 22, 2025
3 checks passed
@JDIZM JDIZM deleted the refactor-response-error-handling branch October 22, 2025 15:59
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

Comments