Improved error handling
parent
196a6f54f2
commit
45fd265264
14
lib.go
14
lib.go
|
@ -4,10 +4,10 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"fmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cli struct {
|
type Cli struct {
|
||||||
|
@ -56,11 +56,15 @@ func (c *Cli) Do(method string, strurl string, body []byte) (*http.Response, err
|
||||||
req.Header.Set(k, v)
|
req.Header.Set(k, v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res,err:= c.cli.Do(req)
|
res, err := c.cli.Do(req)
|
||||||
if err==nil && res.StatusCode >=400{
|
if err != nil {
|
||||||
return nil, errors.New(fmt.Sprintf("Http return code - %d: %s",res.StatusCode,res.Status))
|
return nil, err
|
||||||
}
|
}
|
||||||
return res,err
|
defer res.Body.Close()
|
||||||
|
if res.StatusCode >= 400 {
|
||||||
|
return nil, errors.New(fmt.Sprintf("Http return code - %d: %s", res.StatusCode, res.Status))
|
||||||
|
}
|
||||||
|
return res, err
|
||||||
}
|
}
|
||||||
func (c *Cli) DoJson(method string, strurl string, i interface{}, o interface{}) (err error) {
|
func (c *Cli) DoJson(method string, strurl string, i interface{}, o interface{}) (err error) {
|
||||||
bs, err := json.Marshal(i)
|
bs, err := json.Marshal(i)
|
||||||
|
|
Loading…
Reference in New Issue