Skip to content

fix: Standardize Error Handling in Postgres Package#32

Merged
abolfazl-alemi-aa merged 2 commits intomainfrom
fix/refactor-postgres-error-handling
Dec 18, 2025
Merged

fix: Standardize Error Handling in Postgres Package#32
abolfazl-alemi-aa merged 2 commits intomainfrom
fix/refactor-postgres-error-handling

Conversation

@abolfazl-alemi-aa
Copy link
Copy Markdown
Collaborator

Summary

Updated the v1/postgres package to ensure consistent error handling across all methods. All functions now return GORM errors directly, with comprehensive documentation and test coverage demonstrating recommended patterns.

Changes

Documentation Updates

  • Updated package-level error handling documentation in doc.go
  • Updated method comments in 6 files to specify "GORM error" return type

Test Enhancements

  • Added comprehensive error handling test suite with 4 test patterns:
    • Direct GORM error checking
    • Helper function pattern (recommended)
    • Error translation (optional)
    • Constraint violation handling

Key Improvements

  • Consistency: All methods (direct and QueryBuilder) return GORM errors
  • Clarity: Every method documents its error return type
  • Best Practices: Three recommended patterns documented with examples
  • Test Coverage: Executable examples validate all patterns

Recommended Error Handling

Pattern 1: Direct GORM Errors

err := db.First(ctx, &user, conditions)
if errors.Is(err, gorm.ErrRecordNotFound) {
    // Handle not found
}

Pattern 2: Helper Functions

func isRecordNotFound(err error) bool {
    return errors.Is(err, gorm.ErrRecordNotFound)
}

if isRecordNotFound(err) {
    // Handle consistently
}

Pattern 3: Translation (When Needed)

err = db.TranslateError(err)
if errors.Is(err, postgres.ErrRecordNotFound) {
    // Use standardized error
}

…rror propagation

Updated all Postgres methods and QueryBuilder implementations to return GORM errors directly for consistency and performance. Introduced optional `TranslateError` utility for standardized error conversion when needed. Enhanced documentation and integration tests to demonstrate direct GORM error handling, translation patterns, and helper functions for consistency in error handling workflows.
@abolfazl-alemi-aa abolfazl-alemi-aa merged commit 4af697c into main Dec 18, 2025
3 checks passed
@abolfazl-alemi-aa abolfazl-alemi-aa deleted the fix/refactor-postgres-error-handling branch December 18, 2025 11:19
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