better error handling and slog response
This commit is contained in:
parent
f1bb00ef09
commit
05ca5ac787
6
service_a/internal/domains/common.go
Normal file
6
service_a/internal/domains/common.go
Normal file
@ -0,0 +1,6 @@
|
||||
package domains
|
||||
|
||||
var (
|
||||
SQLSTATE_25P02 = "25P02"
|
||||
SQLSTATE_23505 = "23505"
|
||||
)
|
||||
@ -27,20 +27,23 @@ func NewHandler(service *Service) *Handler {
|
||||
func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseMultipartForm(10 << 20)
|
||||
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
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
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
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
content, err := io.ReadAll(file)
|
||||
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
|
||||
}
|
||||
|
||||
@ -57,13 +60,13 @@ func (h *Handler) IngestCSV(w http.ResponseWriter, r *http.Request) {
|
||||
slog.Error(ErrCannotParseFile.Error(),
|
||||
"filename", header.Filename,
|
||||
"error", err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
h.ToJSON(w, http.StatusConflict, app.H{"error": err})
|
||||
return
|
||||
}
|
||||
fileStats.ElapsedMS = int(time.Since(start).Milliseconds())
|
||||
h.s.UpdateElapsedMS(r.Context(), fileStats.BatchID, fileStats.ElapsedMS)
|
||||
|
||||
slog.Info("CSV file processed",
|
||||
slog.Info("csv file processed",
|
||||
"filename", header.Filename,
|
||||
"rows_inserted", fileStats.RowsInserted,
|
||||
"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) {
|
||||
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) {
|
||||
@ -94,7 +99,7 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
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()})
|
||||
return
|
||||
}
|
||||
@ -106,7 +111,7 @@ func (h *Handler) GetMeteoData(w http.ResponseWriter, r *http.Request) {
|
||||
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})
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user