- Archiver 131 .md dans docs/archive/root-md/ - Archiver 22 .json dans docs/archive/root-json/ - Conserver 7 .md utiles (README, CONTRIBUTING, CHANGELOG, etc.) - Conserver package.json, package-lock.json, turbo.json - Ajouter README d'index dans chaque archive
4.9 KiB
Error Response Format Standardization
INT-006: Standardize error response format
Date: 2025-12-25
Status: Completed
Summary
All error responses in the Veza API now use a consistent, standardized format that matches the APIResponse envelope structure.
Standard Error Response Format
All error responses follow this structure:
{
"success": false,
"data": null,
"error": {
"code": 1000,
"message": "Error message",
"details": [
{
"field": "email",
"message": "Invalid email format"
}
],
"request_id": "550e8400-e29b-41d4-a716-446655440000",
"timestamp": "2025-12-25T10:30:00Z",
"context": {
"user_id": "123e4567-e89b-12d3-a456-426614174000"
}
}
}
Error Object Fields
- code (number, required): Error code from the error code system (1000-9999)
- message (string, required): Human-readable error message
- details (array, optional): Array of validation error details
- field (string): Field name that failed validation
- message (string): Field-specific error message
- value (string, optional): Invalid value that was provided
- request_id (string, optional): Request ID for tracking and debugging
- timestamp (string, required): ISO 8601 timestamp of when the error occurred
- context (object, optional): Additional context information (user_id, etc.)
Implementation
Backend
All error responses are now standardized through:
-
RespondWithAppError(internal/handlers/error_response.go):- Primary function for sending standardized error responses
- Uses
AppErrortype frominternal/errors - Automatically includes request_id, timestamp, and context
-
Error Handler Middleware (
internal/middleware/error_handler.go):- Catches all unhandled errors
- Converts them to standardized format
- Uses
APIResponseenvelope structure
-
Handler Functions:
- All handlers use
RespondWithAppErrorfor errors - No direct
gin.H{"error": ...}usage - Consistent error handling across all endpoints
- All handlers use
Frontend
The frontend already handles the standardized format through:
-
parseApiError(apps/web/src/utils/apiErrorHandler.ts):- Parses backend error responses
- Handles multiple response formats (legacy and new)
- Normalizes to
ApiErrortype
-
ApiErrorType (apps/web/src/types/api.ts):- Matches backend error structure
- Includes all standard fields
-
Error Handling:
- Axios interceptors handle error responses
- User-friendly error messages displayed
- Request ID available for debugging
Error Code System
Error codes follow a hierarchical system:
- 1000-1999: Authentication & Authorization errors
- 2000-2999: Validation errors
- 3000-3999: Resource errors (not found, conflict, etc.)
- 4000-4999: Business logic errors
- 5000-5099: Rate limiting errors
- 6000-6999: External service errors
- 9000-9999: Internal server errors
Changes Made
Backend Changes
-
internal/middleware/error_handler.go:- Updated to use
APIResponseenvelope - All error responses now include
success: false - Standardized error object structure
- Added timestamp to all error responses
- Updated to use
-
internal/handlers/webhook_handlers.go:- Replaced all
gin.H{"error": ...}withRespondWithAppError - Consistent error handling across all webhook endpoints
- Proper error codes and messages
- Replaced all
-
Error Response Helpers:
RespondWithAppError: Main function for standardized errorsRespondWithError: Alternative for simple errors- Both use
APIResponseenvelope
Frontend Compatibility
The frontend already supports the standardized format:
parseApiErrorhandles{success: false, error: {...}}formatApiErrortype matches backend structure- Error messages displayed correctly to users
Testing
All error responses should:
- Use
APIResponseenvelope withsuccess: false - Include all required error fields (code, message, timestamp)
- Include optional fields when available (request_id, details, context)
- Use appropriate HTTP status codes
- Be parseable by frontend
parseApiErrorfunction
Migration Notes
- Legacy error formats (
gin.H{"error": ...}) have been replaced - All handlers now use
RespondWithAppError - Middleware ensures unhandled errors are standardized
- Frontend handles both old and new formats for backward compatibility
Files Modified
veza-backend-api/internal/middleware/error_handler.goveza-backend-api/internal/handlers/webhook_handlers.go- Created:
ERROR_RESPONSE_STANDARD.md(this document)
Next Steps
- ✅ Standardize middleware error handler
- ✅ Update webhook handlers
- ✅ Verify frontend compatibility
- ⏳ Audit other handlers for non-standard error responses
- ⏳ Add integration tests for error format validation