commands: Added error marshalling to Response
Matt Bell committed
Oct 10, 2014 at 13:59 UTC
aa592cec8013edba9c4aeae77028acc2732dc02c
1 file changed
+12
-3
commands/response.go
+12
-3
@@ -16,8 +16,8 @@ const (
16
17
// Error is a struct for marshalling errors
18
type Error struct {
19
- message string
20
- code ErrorType
19
+ Message string
20
+ Code ErrorType
21
}
22
23
type EncodingType string
@@ -50,6 +50,10 @@ func (r *Response) FormatError() Error {
50
}
51
52
func (r *Response) Marshal() ([]byte, error) {
53
+ if r.Error == nil && r.Value == nil {
54
+ return nil, fmt.Errorf("No error or value set, there is nothing to marshal")
55
+ }
56
+
57
enc := r.req.Option("enc")
58
if enc == nil {
59
return nil, fmt.Errorf("No encoding type was specified")
@@ -61,5 +65,10 @@ func (r *Response) Marshal() ([]byte, error) {
65
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
66
}
67
64
- return marshaller(r.Value)
68
+ if r.Error != nil {
69
+ err := r.FormatError()
70
+ return marshaller(err)
71
+ } else {
72
+ return marshaller(r.Value)
73
+ }
74
}