1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

commands/http: Improved client error handling

This commit is contained in:
Matt Bell
2014-10-30 21:56:16 -07:00
committed by Juan Batiz-Benet
parent ea09268044
commit abcebb0bc2

View File

@ -1,8 +1,10 @@
package http
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
@ -65,10 +67,26 @@ func Send(req cmds.Request) (cmds.Response, error) {
if httpRes.StatusCode >= http.StatusBadRequest {
e := cmds.Error{}
err = dec.Decode(&e)
if err != nil {
fmt.Println(err)
return nil, err
if httpRes.StatusCode == http.StatusNotFound {
// handle 404s
e.Message = "Command not found."
e.Code = cmds.ErrClient
} else if contentType == "text/plain" {
// handle non-marshalled errors
buf := bytes.NewBuffer(nil)
io.Copy(buf, httpRes.Body)
e.Message = string(buf.Bytes())
e.Code = cmds.ErrNormal
} else {
// handle marshalled errors
err = dec.Decode(&e)
if err != nil {
fmt.Println(err)
return nil, err
}
}
res.SetError(e, e.Code)