forked from ipfs/go-ipfs-cmds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.go
62 lines (50 loc) · 1.2 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package cmds
import (
"fmt"
"io"
"github.com/ipfs/go-ipfs-cmdkit"
)
var (
ErrRcvdError = fmt.Errorf("received command error")
)
// Response is the result of a command request. Response is returned to the client.
type Response interface {
Request() *Request
Error() *cmdkit.Error
Length() uint64
// Next returns the next emitted value.
// The returned error can be a network or decoding error.
// The error can also be ErrRcvdError if an error has been emitted.
// In this case the emitted error can be accessed using the Error() method.
Next() (interface{}, error)
RawNext() (interface{}, error)
}
type Head struct {
Len uint64
Err *cmdkit.Error
}
func (h Head) Length() uint64 {
return h.Len
}
func (h Head) Error() *cmdkit.Error {
return h.Err
}
// HandleError handles the error from cmds.Response.Next(), it returns
// true if Next() should be called again
func HandleError(err error, res Response, re ResponseEmitter) bool {
if err != nil {
if err == io.EOF {
return false
}
if err == ErrRcvdError {
err = res.Error()
}
if e, ok := err.(*cmdkit.Error); ok {
re.SetError(e.Message, e.Code)
} else {
re.SetError(err, cmdkit.ErrNormal)
}
return false
}
return true
}