Added structured logging across services and repositories. Updated SQL queries to use parameterized placeholders for better readability and security. Enhanced error handling for external service communication.
This commit is contained in:
@@ -3,10 +3,12 @@ package services
|
||||
import (
|
||||
"FamilyHub/src/domain"
|
||||
"FamilyHub/src/repositories"
|
||||
"FamilyHub/src/utils"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -38,7 +40,10 @@ var (
|
||||
)
|
||||
|
||||
func (s *transactionService) Create(ctx context.Context, req domain.CreateTransactionRequest) (*domain.Transaction, error) {
|
||||
log.Printf("transaction create request: payload=%s", utils.ToLogJSON(req))
|
||||
|
||||
if strings.TrimSpace(req.Type) == "" || strings.TrimSpace(req.Category) == "" {
|
||||
log.Printf("transaction create failed: err=%v payload=%s", ErrInvalidTransaction, utils.ToLogJSON(req))
|
||||
return nil, ErrInvalidTransaction
|
||||
}
|
||||
|
||||
@@ -55,8 +60,10 @@ func (s *transactionService) Create(ctx context.Context, req domain.CreateTransa
|
||||
|
||||
if err := s.repo.Create(ctx, transaction); err != nil {
|
||||
if errors.Is(err, repositories.ErrReceiptNotFound) {
|
||||
log.Printf("transaction create failed: err=%v payload=%s", ErrReceiptNotFound, utils.ToLogJSON(req))
|
||||
return nil, ErrReceiptNotFound
|
||||
}
|
||||
log.Printf("transaction create failed: err=%v payload=%s", err, utils.ToLogJSON(req))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -72,6 +79,7 @@ func (s *transactionService) Create(ctx context.Context, req domain.CreateTransa
|
||||
})
|
||||
}
|
||||
|
||||
log.Printf("transaction create response: payload=%s", utils.ToLogJSON(transaction))
|
||||
return transaction, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user