Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error to client API when http status >= 400 #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions zap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const (
DefaultHTTPSBase = "https://zap/JSON/"
DefaultHTTPSBaseOther = "https://zap/OTHER/"
DefaultProxy = "tcp://127.0.0.1:8080"
NoHTTPStatus = -1
ZAP_API_KEY_PARAM = "apikey"
ZAP_API_KEY_HEADER = "X-ZAP-API-Key"
)
Expand Down Expand Up @@ -83,7 +84,7 @@ func NewClient(cfg *Config) (Interface, error) {

// Request sends HTTP request to zap base("http://zap/JSON/") API group
func (c *Client) Request(path string, queryParams map[string]string) (map[string]interface{}, error) {
body, err := c.request(c.Base+path, queryParams)
status, body, err := c.request(c.Base+path, queryParams)
if err != nil {
return nil, err
}
Expand All @@ -93,18 +94,25 @@ func (c *Client) Request(path string, queryParams map[string]string) (map[string
if err := json.Unmarshal(body, &obj); err != nil {
return nil, err
}
return obj, nil
// set error as indicated by http status code
if status == NoHTTPStatus || status >= 400 {
// post the full response, vs only obj["code"] and obj["message"]
err = fmt.Errorf("API http status: %v, response: %v", status, obj)
}
return obj, err
}

// RequestOther sends HTTP request to zap other("http://zap/OTHER/") API group
func (c *Client) RequestOther(path string, queryParams map[string]string) ([]byte, error) {
return c.request(c.BaseOther+path, queryParams)
_, r, err := c.request(c.BaseOther+path, queryParams)
return r, err
}

func (c *Client) request(path string, queryParams map[string]string) ([]byte, error) {
// request returns NoHTTPStatus on failure, message response bytes, and error if applicable
func (c *Client) request(path string, queryParams map[string]string) (int, []byte, error) {
req, err := http.NewRequest("GET", path, nil)
if err != nil {
return nil, err
return NoHTTPStatus, nil, err
}

if len(queryParams) == 0 {
Expand All @@ -131,8 +139,9 @@ func (c *Client) request(path string, queryParams map[string]string) ([]byte, er

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("Errored when sending request to the server: %v", err)
return NoHTTPStatus, nil, fmt.Errorf("Errored when sending request to the server: %v", err)
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
r, err := ioutil.ReadAll(resp.Body)
return resp.StatusCode, r, err
}