@@ -3,6 +3,7 @@ package blockstoreutil
3
3
4
4
import (
5
5
"context"
6
+ "errors"
6
7
"fmt"
7
8
"io"
8
9
@@ -13,14 +14,14 @@ import (
13
14
)
14
15
15
16
// 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
18
19
// reason the block could not be removed. If the removal was aborted
19
20
// due to a fatal error, Hash will be empty, Error will contain the
20
21
// reason, and no more results will be sent.
21
22
type RemovedBlock struct {
22
- Hash string `json:",omitempty"`
23
- Error string `json:",omitempty"`
23
+ Hash string
24
+ Error error
24
25
}
25
26
26
27
// RmBlocksOpts is used to wrap options for RmBlocks().
@@ -51,17 +52,17 @@ func RmBlocks(ctx context.Context, blocks bs.GCBlockstore, pins pin.Pinner, cids
51
52
// remove this sometime in the future.
52
53
has , err := blocks .Has (ctx , c )
53
54
if err != nil {
54
- out <- & RemovedBlock {Hash : c .String (), Error : err . Error () }
55
+ out <- & RemovedBlock {Hash : c .String (), Error : err }
55
56
continue
56
57
}
57
58
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 }}
59
60
continue
60
61
}
61
62
62
63
err = blocks .DeleteBlock (ctx , c )
63
64
if err != nil {
64
- out <- & RemovedBlock {Hash : c .String (), Error : err . Error () }
65
+ out <- & RemovedBlock {Hash : c .String (), Error : err }
65
66
} else if ! opts .Quiet {
66
67
out <- & RemovedBlock {Hash : c .String ()}
67
68
}
@@ -79,7 +80,7 @@ func FilterPinned(ctx context.Context, pins pin.Pinner, out chan<- interface{},
79
80
stillOkay := make ([]cid.Cid , 0 , len (cids ))
80
81
res , err := pins .CheckIfPinned (ctx , cids ... )
81
82
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 )}
83
84
return nil
84
85
}
85
86
for _ , r := range res {
@@ -88,7 +89,7 @@ func FilterPinned(ctx context.Context, pins pin.Pinner, out chan<- interface{},
88
89
} else {
89
90
out <- & RemovedBlock {
90
91
Hash : r .Key .String (),
91
- Error : r .String (),
92
+ Error : errors . New ( r .String () ),
92
93
}
93
94
}
94
95
}
@@ -107,11 +108,11 @@ func ProcRmOutput(next func() (interface{}, error), sout io.Writer, serr io.Writ
107
108
return err
108
109
}
109
110
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 {
113
114
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 )
115
116
} else {
116
117
fmt .Fprintf (sout , "removed %s\n " , r .Hash )
117
118
}
0 commit comments