Skip to content

Commit a30c487

Browse files
committed
Eliminate emit closure, so something with errors on call to emit.
License: MIT Signed-off-by: Kevin Atkinson <[email protected]>
1 parent 1def0e9 commit a30c487

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

core/commands/cid.go

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,17 @@ func (i *argumentIterator) err() error {
156156

157157
func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts) {
158158
itr := argumentIterator{req.Arguments, req.BodyArgs()}
159-
for {
159+
var emitErr error
160+
for emitErr != nil {
160161
cidStr, ok := itr.next()
161162
if !ok {
162163
break
163164
}
164-
emit := func(fmtd string, err error) {
165-
res := &CidFormatRes{CidStr: cidStr, Formatted: fmtd}
166-
if err != nil {
167-
res.ErrorMsg = err.Error()
168-
}
169-
resp.Emit(res)
170-
}
165+
res := &CidFormatRes{CidStr: cidStr}
171166
c, err := cid.Decode(cidStr)
172167
if err != nil {
173-
emit("", err)
168+
res.ErrorMsg = err.Error()
169+
emitErr = resp.Emit(res)
174170
continue
175171
}
176172
base := opts.newBase
@@ -180,17 +176,26 @@ func emitCids(req *cmds.Request, resp cmds.ResponseEmitter, opts cidFormatOpts)
180176
if opts.verConv != nil {
181177
c, err = opts.verConv(c)
182178
if err != nil {
183-
emit("", err)
179+
res.ErrorMsg = err.Error()
180+
emitErr = resp.Emit(res)
184181
continue
185182
}
186183
}
187184
str, err := cidutil.Format(opts.fmtStr, base, c)
188185
if _, ok := err.(cidutil.FormatStringError); ok {
189186
// no point in continuing if there is a problem with the format string
190187
resp.SetError(err, cmdkit.ErrNormal)
191-
return
192188
}
193-
emit(str, err)
189+
if err != nil {
190+
res.ErrorMsg = err.Error()
191+
} else {
192+
res.Formatted = str
193+
}
194+
emitErr = resp.Emit(res)
195+
}
196+
if emitErr != nil {
197+
resp.SetError(emitErr, cmdkit.ErrFatal)
198+
return
194199
}
195200
err := itr.err()
196201
if err != nil {

0 commit comments

Comments
 (0)