better error handling and slog response

This commit is contained in:
2025-10-29 17:02:12 +01:00
parent f1bb00ef09
commit 05ca5ac787
3 changed files with 27 additions and 8 deletions
@@ -3,6 +3,8 @@ package meteo
import (
"context"
"fmt"
"servicea/internal/domains"
"strings"
b "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
@@ -76,6 +78,9 @@ func (pgx *pgxRepo) insertBatch(ctx context.Context, tx b.Tx, fileChecksum strin
var batchID int
err := tx.QueryRow(ctx, insertBatch, 0, fileChecksum).Scan(&batchID)
if err != nil {
if strings.Contains(err.Error(), domains.SQLSTATE_23505) {
return 0, ErrRecordAlreadyExists
}
return 0, fmt.Errorf("error inserting batch: %w", err)
}
return batchID, nil
@@ -102,6 +107,9 @@ func (pgx *pgxRepo) insertAcceptedMeteoData(ctx context.Context, tx b.Tx, batchI
rowsInserted++
if err != nil {
results.Close()
if strings.Contains(err.Error(), domains.SQLSTATE_23505) {
return 0, ErrRecordAlreadyExists
}
return 0, fmt.Errorf("error executing batch command %d: %w", i, err)
}
}