Skip to content

Commit 5bd3afc

Browse files
AchoArnoldCopilot
andcommitted
fix(api): handle rows.Err() and uuid.Parse errors in libsql repositories
- Add rows.Err() check after iteration loop in Index to catch network errors or timeouts that silently end iteration - Propagate uuid.Parse errors in scanHeartbeat and scanHeartbeatRow instead of discarding them with _ - Propagate uuid.Parse errors in scanHeartbeatMonitorRow for both monitor ID and phone ID fields Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b01227a commit 5bd3afc

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

api/pkg/repositories/libsql_heartbeat_monitor_repository.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,14 @@ func scanHeartbeatMonitorRow(row *sql.Row) (*entities.HeartbeatMonitor, error) {
185185
if err != nil {
186186
return nil, err
187187
}
188-
monitor.ID, _ = uuid.Parse(id)
189-
monitor.PhoneID, _ = uuid.Parse(phoneID)
188+
monitor.ID, err = uuid.Parse(id)
189+
if err != nil {
190+
return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse heartbeat monitor ID [%s]", id))
191+
}
192+
monitor.PhoneID, err = uuid.Parse(phoneID)
193+
if err != nil {
194+
return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse heartbeat monitor phone ID [%s]", phoneID))
195+
}
190196
monitor.UserID = entities.UserID(userID)
191197
monitor.PhoneOnline = phoneOnline != 0
192198
return monitor, nil

api/pkg/repositories/libsql_heartbeat_repository.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ func NewLibsqlHeartbeatRepository(
3333
}
3434

3535
func (repository *libsqlHeartbeatRepository) Store(ctx context.Context, heartbeat *entities.Heartbeat) error {
36-
ctx, span := repository.tracer.Start(ctx)
36+
ctx, span, ctxLogger := repository.tracer.StartWithLogger(ctx, repository.logger)
3737
defer span.End()
3838

39+
ctxLogger.Trace("saving new heartbeat")
40+
3941
ctx, cancel := context.WithTimeout(ctx, dbOperationDuration)
4042
defer cancel()
4143

@@ -93,6 +95,10 @@ func (repository *libsqlHeartbeatRepository) Index(ctx context.Context, userID e
9395
}
9496
heartbeats = append(heartbeats, *heartbeat)
9597
}
98+
if rowsErr := rows.Err(); rowsErr != nil {
99+
msg := fmt.Sprintf("error iterating heartbeat rows for owner [%s]", owner)
100+
return nil, repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(rowsErr, msg))
101+
}
96102

97103
return &heartbeats, nil
98104
}
@@ -147,7 +153,10 @@ func scanHeartbeat(rows *sql.Rows) (*entities.Heartbeat, error) {
147153
if err != nil {
148154
return nil, err
149155
}
150-
heartbeat.ID, _ = uuid.Parse(id)
156+
heartbeat.ID, err = uuid.Parse(id)
157+
if err != nil {
158+
return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse heartbeat ID [%s]", id))
159+
}
151160
heartbeat.Charging = charging != 0
152161
heartbeat.UserID = entities.UserID(userID)
153162
return heartbeat, nil
@@ -162,7 +171,10 @@ func scanHeartbeatRow(row *sql.Row) (*entities.Heartbeat, error) {
162171
if err != nil {
163172
return nil, err
164173
}
165-
heartbeat.ID, _ = uuid.Parse(id)
174+
heartbeat.ID, err = uuid.Parse(id)
175+
if err != nil {
176+
return nil, stacktrace.Propagate(err, fmt.Sprintf("cannot parse heartbeat ID [%s]", id))
177+
}
166178
heartbeat.Charging = charging != 0
167179
heartbeat.UserID = entities.UserID(userID)
168180
return heartbeat, nil

0 commit comments

Comments
 (0)