Конвенция для обработки ошибок + рефакторинг кода

This commit is contained in:
av
2026-06-28 21:22:12 +03:00
parent c6daba46d9
commit 3d5df62d62
14 changed files with 265 additions and 51 deletions
+9 -6
View File
@@ -225,7 +225,7 @@ func (w *Worker) Apply(ctx context.Context, id int64) error {
return fmt.Errorf("apply: %w", err)
}
if d.State != store.StateReview && d.State != store.StateDeferred {
return fmt.Errorf("apply: download %d is in state %s (expected review/deferred)", id, d.State)
return fmt.Errorf("apply: download %d is in state %s (expected review/deferred): %w", id, d.State, ErrConflict)
}
ctx = w.scoped(ctx, capFileLayout, id, d.Infohash.String)
@@ -234,8 +234,11 @@ func (w *Worker) Apply(ctx context.Context, id int64) error {
return fmt.Errorf("apply: %w", err)
}
t, ok, err := w.torrentByInfohash(ctx, d.Infohash.String)
if err != nil || !ok {
return fmt.Errorf("apply: torrent not found: %v", err)
if err != nil {
return fmt.Errorf("apply: lookup torrent: %w", err)
}
if !ok {
return fmt.Errorf("apply: torrent not found")
}
w.transition(ctx, *d, store.StateLinking, "", "")
@@ -304,7 +307,7 @@ func (w *Worker) Relink(ctx context.Context, id int64) error {
return fmt.Errorf("relink: %w", err)
}
if d.State != store.StateReverted && d.State != store.StateCancelled {
return fmt.Errorf("relink: download %d is in state %s (expected reverted/cancelled)", id, d.State)
return fmt.Errorf("relink: download %d is in state %s (expected reverted/cancelled): %w", id, d.State, ErrConflict)
}
if !d.Infohash.Valid {
return fmt.Errorf("relink: download %d has no infohash", id)
@@ -462,7 +465,7 @@ func (w *Worker) Undo(ctx context.Context, id int64) error {
return fmt.Errorf("undo: %w", err)
}
if d.State != store.StateDone {
return fmt.Errorf("undo: download %d is in state %s (expected done)", id, d.State)
return fmt.Errorf("undo: download %d is in state %s (expected done): %w", id, d.State, ErrConflict)
}
ctx = w.scoped(ctx, capFileLayout, id, d.Infohash.String)
batch, err := w.store.LatestBatchID(ctx, id)
@@ -499,7 +502,7 @@ func (w *Worker) requireReviewable(ctx context.Context, id int64, op string) (*s
return nil, fmt.Errorf("%s: %w", op, err)
}
if d.State != store.StateReview && d.State != store.StateDeferred {
return nil, fmt.Errorf("%s: download %d is in state %s (expected review/deferred)", op, id, d.State)
return nil, fmt.Errorf("%s: download %d is in state %s (expected review/deferred): %w", op, id, d.State, ErrConflict)
}
return d, nil
}