Skip to content

Commit 4b71de8

Browse files
committed
fix(history): handle json marshal errors, add logs
- Fixes errors in `RecordExecution` when failing to marshal headers/params - Adds debug logs for validation failures - Adds error log for timestamp parsing
1 parent a400892 commit 4b71de8

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

internal/crud/interfaces.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package crud provides generic CRUD operations for entities.
12
package crud
23

34
import (

internal/crud/interfaces_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func TestCRUDValidation(t *testing.T) {
180180
})
181181
}
182182

183-
// Mock implementation for testing
184183
type testCRUDManager struct {
185184
entities map[int64]TestEntity
186185
nextID int64

internal/crud/utils.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,26 @@ package crud
33
import (
44
"fmt"
55
"strings"
6+
7+
"github.com/maniac-en/req/internal/log"
68
)
79

810
func ValidateName(name string) error {
911
name = strings.TrimSpace(name)
1012
if name == "" {
13+
log.Debug("validation failed: empty name")
1114
return fmt.Errorf("name cannot be empty")
1215
}
1316
if len(name) > 100 {
17+
log.Debug("validation failed: name too long", "length", len(name))
1418
return fmt.Errorf("name cannot exceed 100 characters")
1519
}
1620
return nil
1721
}
1822

1923
func ValidateID(id int64) error {
2024
if id <= 0 {
25+
log.Debug("validation failed: invalid ID", "id", id)
2126
return fmt.Errorf("ID must be positive")
2227
}
2328
return nil

internal/history/manager.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func (h *HistoryManager) ListByCollection(ctx context.Context, collectionID int6
9595
}
9696

9797
// Calculate pagination metadata
98-
totalPages := int((total + int64(limit) - 1) / int64(limit))
98+
totalPages := int((total + int64(limit) - 1) / int64(limit)) // Ceiling division
9999
currentPage := (offset / limit) + 1
100100
hasNext := (offset + limit) < int(total)
101101
hasPrev := offset > 0
@@ -123,9 +123,21 @@ func (h *HistoryManager) RecordExecution(ctx context.Context, data ExecutionData
123123

124124
log.Debug("recording execution", "method", data.Method, "url", data.URL, "status", data.StatusCode)
125125

126-
requestHeaders, _ := json.Marshal(data.Headers)
127-
queryParams, _ := json.Marshal(data.QueryParams)
128-
responseHeaders, _ := json.Marshal(data.ResponseHeaders)
126+
// Marshal to JSON for database storage
127+
requestHeaders, err := json.Marshal(data.Headers)
128+
if err != nil {
129+
return HistoryEntity{}, fmt.Errorf("failed to marshal request headers: %w", err)
130+
}
131+
132+
queryParams, err := json.Marshal(data.QueryParams)
133+
if err != nil {
134+
return HistoryEntity{}, fmt.Errorf("failed to marshal query params: %w", err)
135+
}
136+
137+
responseHeaders, err := json.Marshal(data.ResponseHeaders)
138+
if err != nil {
139+
return HistoryEntity{}, fmt.Errorf("failed to marshal response headers: %w", err)
140+
}
129141

130142
params := database.CreateHistoryEntryParams{
131143
CollectionID: sql.NullInt64{Int64: data.CollectionID, Valid: data.CollectionID > 0},
@@ -156,14 +168,17 @@ func (h *HistoryManager) RecordExecution(ctx context.Context, data ExecutionData
156168

157169
func validateExecutionData(data ExecutionData) error {
158170
if err := crud.ValidateName(data.Method); err != nil {
171+
log.Debug("execution validation failed: invalid method", "method", data.Method)
159172
return fmt.Errorf("invalid method: %w", err)
160173
}
161174

162175
if err := crud.ValidateName(data.URL); err != nil {
176+
log.Debug("execution validation failed: invalid URL", "url", data.URL)
163177
return fmt.Errorf("invalid URL: %w", err)
164178
}
165179

166180
if data.StatusCode < 100 || data.StatusCode > 599 {
181+
log.Debug("execution validation failed: invalid status code", "status_code", data.StatusCode)
167182
return fmt.Errorf("invalid status code: %d", data.StatusCode)
168183
}
169184

internal/history/manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func setupTestDB(t *testing.T) *database.Queries {
1616
t.Fatalf("failed to open in-memory database: %v", err)
1717
}
1818

19-
// Create history table
19+
// Use same schema as migration
2020
schema := `
2121
CREATE TABLE history (
2222
id INTEGER PRIMARY KEY AUTOINCREMENT,

internal/history/models.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
// Package history tracks HTTP request execution records.
12
package history
23

34
import (
45
"time"
56

67
"github.com/maniac-en/req/internal/database"
8+
"github.com/maniac-en/req/internal/log"
79
)
810

911
type HistoryManager struct {
@@ -23,10 +25,15 @@ func (h HistoryEntity) GetName() string {
2325
}
2426

2527
func (h HistoryEntity) GetCreatedAt() time.Time {
26-
t, _ := time.Parse(time.RFC3339, h.ExecutedAt)
28+
t, err := time.Parse(time.RFC3339, h.ExecutedAt)
29+
if err != nil {
30+
log.Error("failed to parse executed_at timestamp", "executed_at", h.ExecutedAt, "error", err)
31+
return time.Time{}
32+
}
2733
return t
2834
}
2935

36+
// GetUpdatedAt returns creation time since history entries are immutable
3037
func (h HistoryEntity) GetUpdatedAt() time.Time {
3138
return h.GetCreatedAt()
3239
}

0 commit comments

Comments
 (0)