better error handling and slog response

This commit is contained in:
Pedro Pérez 2025-10-29 17:02:12 +01:00
parent f1bb00ef09
commit 05ca5ac787
3 changed files with 27 additions and 8 deletions

View File

@ -0,0 +1,6 @@
package domains
var (
SQLSTATE_25P02 = "25P02"
SQLSTATE_23505 = "23505"
)

View File

@ -27,20 +27,23 @@ func NewHandler(service *Service) *Handler {
func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) { func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
err := r.ParseMultipartForm(10 << 20) err := r.ParseMultipartForm(10 << 20)
if err != nil { if err != nil {
http.Error(w, ErrParsingForm.Error(), http.StatusBadRequest) slog.Error(ErrParsingForm.Error(), "error", err)
h.ToJSON(w, http.StatusBadRequest, app.H{"error": ErrParsingForm})
return return
} }
file, header, err := r.FormFile("file") file, header, err := r.FormFile("file")
if err != nil { if err != nil {
http.Error(w, ErrRetrievingFile.Error(), http.StatusBadRequest) slog.Error(ErrRetrievingFile.Error(), "error", err)
h.ToJSON(w, http.StatusBadRequest, app.H{"error": ErrRetrievingFile})
return return
} }
defer file.Close() defer file.Close()
content, err := io.ReadAll(file) content, err := io.ReadAll(file)
if err != nil { if err != nil {
http.Error(w, ErrReadingFile.Error(), http.StatusInternalServerError) slog.Error(ErrReadingFile.Error(), "error", err)
h.ToJSON(w, http.StatusInternalServerError, app.H{"error": ErrReadingFile})
return return
} }
@ -57,13 +60,13 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
slog.Error(ErrCannotParseFile.Error(), slog.Error(ErrCannotParseFile.Error(),
"filename", header.Filename, "filename", header.Filename,
"error", err) "error", err)
http.Error(w, err.Error(), http.StatusBadRequest) h.ToJSON(w, http.StatusConflict, app.H{"error": err})
return return
} }
fileStats.ElapsedMS = int(time.Since(start).Milliseconds()) fileStats.ElapsedMS = int(time.Since(start).Milliseconds())
h.s.UpdateElapsedMS(r.Context(), fileStats.BatchID, fileStats.ElapsedMS) h.s.UpdateElapsedMS(r.Context(), fileStats.BatchID, fileStats.ElapsedMS)
slog.Info("CSV file processed", slog.Info("csv file processed",
"filename", header.Filename, "filename", header.Filename,
"rows_inserted", fileStats.RowsInserted, "rows_inserted", fileStats.RowsInserted,
"rows_rejected", fileStats.RowsRejected, "rows_rejected", fileStats.RowsRejected,
@ -79,7 +82,9 @@ func (h *Handler) IngestExcel(w http.ResponseWriter, r *http.Request) {
} }
func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetCities(w http.ResponseWriter, r *http.Request) {
h.ToJSON(w, http.StatusOK, app.H{"cities": h.s.GetCities(r.Context())}) cities := h.s.GetCities(r.Context())
slog.Info("cities retrieved", "count", len(cities))
h.ToJSON(w, http.StatusOK, app.H{"cities": cities})
} }
func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) { func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
@ -94,7 +99,7 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
} }
if err := params.Validate(); err != nil { if err := params.Validate(); err != nil {
slog.Error("Error validating struct", "error", err) slog.Error("error validating struct", "error", err)
h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()}) h.ToJSON(w, http.StatusBadRequest, app.H{"error": err.Error()})
return return
} }
@ -106,7 +111,7 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
return return
} }
slog.Info("Data retrieved", "location", params.Location) slog.Info("data retrieved", "location", params.Location)
h.ToJSON(w, http.StatusOK, app.H{"meteo_data": meteoData}) h.ToJSON(w, http.StatusOK, app.H{"meteo_data": meteoData})
} }

View File

@ -3,6 +3,8 @@ package meteo
import ( import (
"context" "context"
"fmt" "fmt"
"servicea/internal/domains"
"strings"
b "github.com/jackc/pgx/v5" b "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool" "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 var batchID int
err := tx.QueryRow(ctx, insertBatch, 0, fileChecksum).Scan(&batchID) err := tx.QueryRow(ctx, insertBatch, 0, fileChecksum).Scan(&batchID)
if err != nil { if err != nil {
if strings.Contains(err.Error(), domains.SQLSTATE_23505) {
return 0, ErrRecordAlreadyExists
}
return 0, fmt.Errorf("error inserting batch: %w", err) return 0, fmt.Errorf("error inserting batch: %w", err)
} }
return batchID, nil return batchID, nil
@ -102,6 +107,9 @@ func (pgx *pgxRepo) insertAcceptedMeteoData(ctx context.Context, tx b.Tx, batchI
rowsInserted++ rowsInserted++
if err != nil { if err != nil {
results.Close() 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) return 0, fmt.Errorf("error executing batch command %d: %w", i, err)
} }
} }