Skip to content

Commit

Permalink
commands: Added error marshalling to Response
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Oct 10, 2014
1 parent 1ed7ef1 commit 77afb23
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions commands/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const (

// Error is a struct for marshalling errors
type Error struct {
message string
code ErrorType
Message string
Code ErrorType
}

type EncodingType string
Expand Down Expand Up @@ -50,6 +50,10 @@ func (r *Response) FormatError() Error {
}

func (r *Response) Marshal() ([]byte, error) {
if r.Error == nil && r.Value == nil {
return nil, fmt.Errorf("No error or value set, there is nothing to marshal")
}

enc := r.req.Option("enc")
if enc == nil {
return nil, fmt.Errorf("No encoding type was specified")
Expand All @@ -61,5 +65,10 @@ func (r *Response) Marshal() ([]byte, error) {
return nil, fmt.Errorf("No marshaller found for encoding type '%s'", enc)
}

return marshaller(r.Value)
if r.Error != nil {
err := r.FormatError()
return marshaller(err)
} else {
return marshaller(r.Value)
}
}

0 comments on commit 77afb23

Please sign in to comment.