Repository navigation
feat: implement machine-readable error codes and enhance error handli… - #52
Merged
pikann merged 6 commits intoMar 25, 2026
Conversation
…ng in authentication flow
Contributor
There was a problem hiding this comment.
Pull request overview
Implements a machine-readable API error code system (error_code) across the Go backend and the web frontend, and updates automated tests to assert on stable error codes instead of brittle message strings.
Changes:
- Added
apierrcodes and auth-domain sentinel errors; updated presenter/handlers/middleware to emiterror_codein the standard response envelope. - Updated integration + handler + e2e tests to validate returned
error_codevalues for common auth/user failure scenarios. - Added frontend error-code utilities and updated the login form to map known auth error codes to user-friendly messages.
Reviewed changes
Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| services/api/internal/apierr/codes.go | Introduces stable backend error code definitions and an apierr.Error wrapper. |
| services/api/internal/domain/auth/errors.go | Adds auth sentinel errors for consistent identification/mapping. |
| services/api/internal/transport/http/presenter/response.go | Extends response envelope with error_code and maps domain errors to codes/statuses. |
| services/api/internal/transport/http/middleware/validate.go | Uses presenter + BAD_REQUEST code for JSON binding failures. |
| services/api/internal/transport/http/middleware/authn.go | Returns AUTH_* codes for missing/invalid access tokens. |
| services/api/internal/transport/http/handler/auth_handler.go | Routes auth failures through presenter error mapping / explicit coded errors. |
| services/api/internal/transport/http/handler/user_handler.go | Routes user failures through presenter error mapping / explicit coded errors. |
| services/api/internal/service/auth/auth_service.go | Replaces string errors with sentinel auth errors in login/refresh flows. |
| services/api/internal/service/auth/auth_service_test.go | Updates service tests to assert sentinel errors via errors.Is. |
| services/api/internal/transport/http/handler/auth_handler_test.go | Updates handler tests to assert error_code values and uses sentinel errors in mocks. |
| services/api/internal/transport/http/handler/user_handler_test.go | Updates handler tests to assert error_code values for common user failures. |
| services/api/test/integration/auth_test.go | Adds helper + asserts AUTH_INVALID_CREDENTIALS on wrong password. |
| services/api/test/integration/user_test.go | Asserts USER_USERNAME_TAKEN on duplicate username create. |
| services/api/test/e2e/api_flow_test.go | Adds assertErrorCode helper and asserts codes in end-to-end flows. |
| apps/web/src/lib/auth-api.ts | Extends envelope typing to include error_code. |
| apps/web/src/lib/api-error.ts | Adds frontend ApiErrorCode constants and getApiErrorCode extractor. |
| apps/web/src/hooks/use-login-form.ts | Uses error codes to display more specific login error messages. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Contributor
2 tasks done
Split `Envelope<T>` into `SuccessEnvelope<T>` / `ApiErrorEnvelope` discriminated union
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This pull request introduces a robust, machine-readable error code system for authentication and user-related API errors, replacing previous string-based and HTTP status code error handling. This standardizes error handling across the backend and frontend, improves testability, and enables the frontend to present more specific error messages to users.
Key changes include:
Backend: Machine-Readable Error Codes
apierrpackage in Go, defining stable error codes (e.g.,AUTH_INVALID_CREDENTIALS,AUTH_MISSING_TOKEN) and a standard error envelope for API responses. The API now returns these codes instead of relying on message strings or only HTTP status codes.ErrInvalidCredentials,ErrTokenInvalid, andErrSessionInvalidated. [1] [2] [3] [4] [5]error_codefield. [1] [2] [3] [4] [5]Frontend: Error Code Handling and Messaging
ApiErrorCodedefinitions and a utility (getApiErrorCode) to extract error codes from API responses in the frontend. This allows the UI to display precise error messages for specific authentication failures.AUTH_INVALID_CREDENTIALS). [1] [2]error_codefield.Testing Improvements
These changes create a more maintainable and user-friendly error handling system across the stack, making it easier to diagnose issues and improve the user experience.…ng in authentication flow