Improved error handling

master
Paulo Simão 2021-10-26 22:35:31 -03:00
parent 196a6f54f2
commit 45fd265264
1 changed files with 9 additions and 5 deletions

8
lib.go
View File

@ -4,10 +4,10 @@ import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"fmt"
)
type Cli struct {
@ -57,7 +57,11 @@ func (c *Cli) Do(method string, strurl string, body []byte) (*http.Response, err
}
}
res, err := c.cli.Do(req)
if err==nil && res.StatusCode >=400{
if err != nil {
return nil, 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