Skip to content

Commit f72110c

Browse files
committed
fix: use error instead of strings as error in blockstoreutil/remove
1 parent e36f28d commit f72110c

File tree

3 files changed

+46
-22
lines changed

3 files changed

+46
-22
lines changed

blocks/blockstoreutil/remove.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package blockstoreutil
33

44
import (
55
"context"
6+
"errors"
67
"fmt"
78
"io"
89

@@ -13,14 +14,14 @@ import (
1314
)
1415

1516
// RemovedBlock is used to represent the result of removing a block.
16-
// If a block was removed successfully, then the Error string will be
17-
// empty. If a block could not be removed, then Error will contain the
17+
// If a block was removed successfully, then the Error will be empty.
18+
// If a block could not be removed, then Error will contain the
1819
// reason the block could not be removed. If the removal was aborted
1920
// due to a fatal error, Hash will be empty, Error will contain the
2021
// reason, and no more results will be sent.
2122
type RemovedBlock struct {
22-
Hash string `json:",omitempty"`
23-
Error string `json:",omitempty"`
23+
Hash string
24+
Error error
2425
}
2526

2627
// RmBlocksOpts is used to wrap options for RmBlocks().
@@ -51,17 +52,17 @@ func RmBlocks(ctx context.Context, blocks bs.GCBlockstore, pins pin.Pinner, cids
5152
// remove this sometime in the future.
5253
has, err := blocks.Has(ctx, c)
5354
if err != nil {
54-
out <- &RemovedBlock{Hash: c.String(), Error: err.Error()}
55+
out <- &RemovedBlock{Hash: c.String(), Error: err}
5556
continue
5657
}
5758
if !has && !opts.Force {
58-
out <- &RemovedBlock{Hash: c.String(), Error: format.ErrNotFound{Cid: c}.Error()}
59+
out <- &RemovedBlock{Hash: c.String(), Error: format.ErrNotFound{Cid: c}}
5960
continue
6061
}
6162

6263
err = blocks.DeleteBlock(ctx, c)
6364
if err != nil {
64-
out <- &RemovedBlock{Hash: c.String(), Error: err.Error()}
65+
out <- &RemovedBlock{Hash: c.String(), Error: err}
6566
} else if !opts.Quiet {
6667
out <- &RemovedBlock{Hash: c.String()}
6768
}
@@ -79,7 +80,7 @@ func FilterPinned(ctx context.Context, pins pin.Pinner, out chan<- interface{},
7980
stillOkay := make([]cid.Cid, 0, len(cids))
8081
res, err := pins.CheckIfPinned(ctx, cids...)
8182
if err != nil {
82-
out <- &RemovedBlock{Error: fmt.Sprintf("pin check failed: %s", err)}
83+
out <- &RemovedBlock{Error: fmt.Errorf("pin check failed: %w", err)}
8384
return nil
8485
}
8586
for _, r := range res {
@@ -88,7 +89,7 @@ func FilterPinned(ctx context.Context, pins pin.Pinner, out chan<- interface{},
8889
} else {
8990
out <- &RemovedBlock{
9091
Hash: r.Key.String(),
91-
Error: r.String(),
92+
Error: errors.New(r.String()),
9293
}
9394
}
9495
}
@@ -107,11 +108,11 @@ func ProcRmOutput(next func() (interface{}, error), sout io.Writer, serr io.Writ
107108
return err
108109
}
109110
r := res.(*RemovedBlock)
110-
if r.Hash == "" && r.Error != "" {
111-
return fmt.Errorf("aborted: %s", r.Error)
112-
} else if r.Error != "" {
111+
if r.Hash == "" && r.Error != nil {
112+
return fmt.Errorf("aborted: %w", r.Error)
113+
} else if r.Error != nil {
113114
someFailed = true
114-
fmt.Fprintf(serr, "cannot remove %s: %s\n", r.Hash, r.Error)
115+
fmt.Fprintf(serr, "cannot remove %s: %v\n", r.Hash, r.Error)
115116
} else {
116117
fmt.Fprintf(sout, "removed %s\n", r.Hash)
117118
}

core/commands/block.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
files "github.com/ipfs/go-ipfs-files"
1010

11-
util "github.com/ipfs/go-ipfs/blocks/blockstoreutil"
1211
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1312
"github.com/ipfs/go-ipfs/core/commands/cmdutils"
1413

@@ -213,6 +212,11 @@ const (
213212
blockQuietOptionName = "quiet"
214213
)
215214

215+
type removedBlock struct {
216+
Hash string `json:",omitempty"`
217+
Error string `json:",omitempty"`
218+
}
219+
216220
var blockRmCmd = &cmds.Command{
217221
Helptext: cmds.HelpText{
218222
Tagline: "Remove IPFS block(s).",
@@ -246,7 +250,7 @@ It takes a list of base58 encoded multihashes to remove.
246250

247251
err = api.Block().Rm(req.Context, rp, options.Block.Force(force))
248252
if err != nil {
249-
if err := res.Emit(&util.RemovedBlock{
253+
if err := res.Emit(&removedBlock{
250254
Hash: rp.Cid().String(),
251255
Error: err.Error(),
252256
}); err != nil {
@@ -256,7 +260,7 @@ It takes a list of base58 encoded multihashes to remove.
256260
}
257261

258262
if !quiet {
259-
err := res.Emit(&util.RemovedBlock{
263+
err := res.Emit(&removedBlock{
260264
Hash: rp.Cid().String(),
261265
})
262266
if err != nil {
@@ -269,8 +273,29 @@ It takes a list of base58 encoded multihashes to remove.
269273
},
270274
PostRun: cmds.PostRunMap{
271275
cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
272-
return util.ProcRmOutput(res.Next, os.Stdout, os.Stderr)
276+
someFailed := false
277+
for {
278+
res, err := res.Next()
279+
if err == io.EOF {
280+
break
281+
} else if err != nil {
282+
return err
283+
}
284+
r := res.(*removedBlock)
285+
if r.Hash == "" && r.Error != "" {
286+
return fmt.Errorf("aborted: %s", r.Error)
287+
} else if r.Error != "" {
288+
someFailed = true
289+
fmt.Fprintf(os.Stderr, "cannot remove %s: %s\n", r.Hash, r.Error)
290+
} else {
291+
fmt.Fprintf(os.Stdout, "removed %s\n", r.Hash)
292+
}
293+
}
294+
if someFailed {
295+
return fmt.Errorf("some blocks not removed")
296+
}
297+
return nil
273298
},
274299
},
275-
Type: util.RemovedBlock{},
300+
Type: removedBlock{},
276301
}

core/coreapi/block.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ func (api *BlockAPI) Rm(ctx context.Context, p path.Path, opts ...caopts.BlockRm
107107
return errors.New("got unexpected output from util.RmBlocks")
108108
}
109109

110-
// Because errors come as strings we lose information about
111-
// the error type.
112-
if remBlock.Error != "" {
113-
return errors.New(remBlock.Error)
110+
if remBlock.Error != nil {
111+
return remBlock.Error
114112
}
115113
return nil
116114
case <-ctx.Done():

0 commit comments

Comments
 (0)