Skip to content

feat: implement machine-readable error codes and enhance error handli… - #52

Merged
pikann merged 6 commits into
31-epic-authentication-authorizationfrom
feature/implement-machine-readable-error-code
Mar 25, 2026
Merged

pikann merged 6 commits into
31-epic-authentication-authorizationfrom
feature/implement-machine-readable-error-code

Conversation

@pikann

@pikann pikann commented Mar 25, 2026 •

Copy link
Copy Markdown
Contributor

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

  • Introduced the apierr package 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.
  • Updated authentication domain and service logic to use sentinel errors and error codes, replacing ambiguous error messages with specific errors like ErrInvalidCredentials, ErrTokenInvalid, and ErrSessionInvalidated. [1] [2] [3] [4] [5]
  • Modified HTTP handlers to use the new error presenter, ensuring error responses include both a human-readable message and a machine-readable error_code field. [1] [2] [3] [4] [5]

Frontend: Error Code Handling and Messaging

  • Added ApiErrorCode definitions 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.
  • Updated the login form hook to use error codes for user feedback, mapping codes to user-friendly messages (e.g., "Invalid username or password" for AUTH_INVALID_CREDENTIALS). [1] [2]
  • Extended API envelope types to include the new error_code field.

Testing Improvements

  • Updated backend tests to assert on specific error codes in API responses, ensuring the correct codes are returned for various authentication error scenarios. Added helpers to decode and verify error envelopes in test responses. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 apierr codes and auth-domain sentinel errors; updated presenter/handlers/middleware to emit error_code in the standard response envelope.
  • Updated integration + handler + e2e tests to validate returned error_code values 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.

Comment thread apps/web/src/lib/auth-api.ts Resolved

Copilot AI commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

@pikann I've opened a new pull request, #53, to work on those changes. Once the pull request is ready, I'll request review from you.

@pikann
pikann merged commit ecff67a into 31-epic-authentication-authorization Mar 25, 2026
5 checks passed
@pikann
pikann deleted the feature/implement-machine-readable-error-code branch March 25, 2026 12:21
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.

3 participants