Skip to content

Commit

Permalink
api: return an error when object get --encoding=protobuf is fails.
Browse files Browse the repository at this point in the history
When `--encoding=protobuf` was set, `object get` used to always return a 200
response and would leave the body blank on error. Now, it returns a JSON error.

fixes ipfs#2468

License: MIT
Signed-off-by: Steven Allen <steven@stebalien.com>
  • Loading branch information
Stebalien committed Mar 30, 2016
1 parent 92ba9af commit afede70
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
10 changes: 10 additions & 0 deletions commands/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ var mimeTypes = map[string]string{
cmds.Text: "text/plain",
}

var errorMimeTypes = map[string]string{
cmds.Protobuf: "application/json",
}

type ServerConfig struct {
// Headers is an optional map of headers that is written out.
Headers map[string][]string
Expand Down Expand Up @@ -206,6 +210,12 @@ func guessMimeType(res cmds.Response) (string, error) {
return "", errors.New("no encoding option set")
}

if res.Error() != nil {
if m, ok := errorMimeTypes[enc]; ok {
return m, nil
}
}

if m, ok := mimeTypes[enc]; ok {
return m, nil
}
Expand Down
28 changes: 19 additions & 9 deletions core/commands/object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,25 @@ This command outputs data in the following encodings:
Type: Node{},
Marshalers: cmds.MarshalerMap{
cmds.Protobuf: func(res cmds.Response) (io.Reader, error) {
node := res.Output().(*Node)
object, err := deserializeNode(node)
if err != nil {
return nil, err
}

marshaled, err := object.Marshal()
if err != nil {
return nil, err
var marshaled []byte

if res.Error() != nil {
output, err := json.Marshal(res.Error())
if err != nil {
return nil, err
}
marshaled = append(output, '\n')
} else {
node := res.Output().(*Node)
object, err := deserializeNode(node)
if err != nil {
return nil, err
}
output, err := object.Marshal()
if err != nil {
return nil, err
}
marshaled = output
}
return bytes.NewReader(marshaled), nil
},
Expand Down

0 comments on commit afede70

Please sign in to comment.