wrap few doRequest calls for error handling (#11158)

pull/11187/head
FFMMM 2021-09-29 13:12:15 -07:00 committed by GitHub
parent 19040586ce
commit 8bb6d8571c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 38 deletions

View File

@ -2,8 +2,8 @@ package api
import ( import (
"bufio" "bufio"
"bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@ -602,6 +602,8 @@ func (a *Agent) AgentHealthServiceByIDOpts(serviceID string, q *QueryOptions) (s
r.setQueryOptions(q) r.setQueryOptions(q)
r.params.Add("format", "json") r.params.Add("format", "json")
r.header.Set("Accept", "application/json") r.header.Set("Accept", "application/json")
// not a lot of value in wrapping the doRequest call in a requireHttpCodes call
// we manipulate the resp body and the require calls "swallow" the content on err
_, resp, err := a.c.doRequest(r) _, resp, err := a.c.doRequest(r)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
@ -641,6 +643,8 @@ func (a *Agent) AgentHealthServiceByNameOpts(service string, q *QueryOptions) (s
r.setQueryOptions(q) r.setQueryOptions(q)
r.params.Add("format", "json") r.params.Add("format", "json")
r.header.Set("Accept", "application/json") r.header.Set("Accept", "application/json")
// not a lot of value in wrapping the doRequest call in a requireHttpCodes call
// we manipulate the resp body and the require calls "swallow" the content on err
_, resp, err := a.c.doRequest(r) _, resp, err := a.c.doRequest(r)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
@ -1233,19 +1237,20 @@ func (a *Agent) updateTokenOnce(target, token string, q *WriteOptions) (*WriteMe
r.setWriteOptions(q) r.setWriteOptions(q)
r.obj = &AgentToken{Token: token} r.obj = &AgentToken{Token: token}
rtt, resp, err := a.c.doRequest(r) rtt, resp, err := requireOK(a.c.doRequest(r))
wm := &WriteMeta{RequestTime: rtt}
if err != nil { if err != nil {
return nil, 0, err // if the error was bc of a non 200 response
// from requireOK
var statusE StatusError
if errors.As(err, &statusE) {
return wm, statusE.Code, statusE
}
// otherwise, the error came via doRequest
return nil, 500, err
} }
defer closeResponseBody(resp) defer closeResponseBody(resp)
wm := &WriteMeta{RequestTime: rtt}
if resp.StatusCode != 200 {
var buf bytes.Buffer
io.Copy(&buf, resp.Body)
return wm, resp.StatusCode, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
}
return wm, resp.StatusCode, nil return wm, resp.StatusCode, nil
} }

View File

@ -25,16 +25,12 @@ func (c *Client) Debug() *Debug {
// Heap returns a pprof heap dump // Heap returns a pprof heap dump
func (d *Debug) Heap() ([]byte, error) { func (d *Debug) Heap() ([]byte, error) {
r := d.c.newRequest("GET", "/debug/pprof/heap") r := d.c.newRequest("GET", "/debug/pprof/heap")
_, resp, err := d.c.doRequest(r) _, resp, err := requireOK(d.c.doRequest(r))
if err != nil { if err != nil {
return nil, fmt.Errorf("error making request: %s", err) return nil, fmt.Errorf("error making request: %s", err)
} }
defer closeResponseBody(resp) defer closeResponseBody(resp)
if resp.StatusCode != 200 {
return nil, generateUnexpectedResponseCodeError(resp)
}
// We return a raw response because we're just passing through a response // We return a raw response because we're just passing through a response
// from the pprof handlers // from the pprof handlers
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
@ -52,16 +48,12 @@ func (d *Debug) Profile(seconds int) ([]byte, error) {
// Capture a profile for the specified number of seconds // Capture a profile for the specified number of seconds
r.params.Set("seconds", strconv.Itoa(seconds)) r.params.Set("seconds", strconv.Itoa(seconds))
_, resp, err := d.c.doRequest(r) _, resp, err := requireOK(d.c.doRequest(r))
if err != nil { if err != nil {
return nil, fmt.Errorf("error making request: %s", err) return nil, fmt.Errorf("error making request: %s", err)
} }
defer closeResponseBody(resp) defer closeResponseBody(resp)
if resp.StatusCode != 200 {
return nil, generateUnexpectedResponseCodeError(resp)
}
// We return a raw response because we're just passing through a response // We return a raw response because we're just passing through a response
// from the pprof handlers // from the pprof handlers
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
@ -81,14 +73,11 @@ func (d *Debug) PProf(ctx context.Context, name string, seconds int) (io.ReadClo
// Capture a profile for the specified number of seconds // Capture a profile for the specified number of seconds
r.params.Set("seconds", strconv.Itoa(seconds)) r.params.Set("seconds", strconv.Itoa(seconds))
_, resp, err := d.c.doRequest(r) _, resp, err := requireOK(d.c.doRequest(r))
if err != nil { if err != nil {
return nil, fmt.Errorf("error making request: %s", err) return nil, fmt.Errorf("error making request: %s", err)
} }
if resp.StatusCode != 200 {
return nil, generateUnexpectedResponseCodeError(resp)
}
return resp.Body, nil return resp.Body, nil
} }
@ -99,16 +88,12 @@ func (d *Debug) Trace(seconds int) ([]byte, error) {
// Capture a trace for the specified number of seconds // Capture a trace for the specified number of seconds
r.params.Set("seconds", strconv.Itoa(seconds)) r.params.Set("seconds", strconv.Itoa(seconds))
_, resp, err := d.c.doRequest(r) _, resp, err := requireOK(d.c.doRequest(r))
if err != nil { if err != nil {
return nil, fmt.Errorf("error making request: %s", err) return nil, fmt.Errorf("error making request: %s", err)
} }
defer closeResponseBody(resp) defer closeResponseBody(resp)
if resp.StatusCode != 200 {
return nil, generateUnexpectedResponseCodeError(resp)
}
// We return a raw response because we're just passing through a response // We return a raw response because we're just passing through a response
// from the pprof handlers // from the pprof handlers
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
@ -123,16 +108,12 @@ func (d *Debug) Trace(seconds int) ([]byte, error) {
func (d *Debug) Goroutine() ([]byte, error) { func (d *Debug) Goroutine() ([]byte, error) {
r := d.c.newRequest("GET", "/debug/pprof/goroutine") r := d.c.newRequest("GET", "/debug/pprof/goroutine")
_, resp, err := d.c.doRequest(r) _, resp, err := requireOK(d.c.doRequest(r))
if err != nil { if err != nil {
return nil, fmt.Errorf("error making request: %s", err) return nil, fmt.Errorf("error making request: %s", err)
} }
defer closeResponseBody(resp) defer closeResponseBody(resp)
if resp.StatusCode != 200 {
return nil, generateUnexpectedResponseCodeError(resp)
}
// We return a raw response because we're just passing through a response // We return a raw response because we're just passing through a response
// from the pprof handlers // from the pprof handlers
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)

View File

@ -129,6 +129,8 @@ func (k *KV) getInternal(key string, params map[string]string, q *QueryOptions)
r.params.Set(param, val) r.params.Set(param, val)
} }
rtt, resp, err := k.c.doRequest(r) rtt, resp, err := k.c.doRequest(r)
rtt, resp, err = requireHttpCodes(rtt, resp, err, 200, 404)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
@ -140,10 +142,8 @@ func (k *KV) getInternal(key string, params map[string]string, q *QueryOptions)
if resp.StatusCode == 404 { if resp.StatusCode == 404 {
closeResponseBody(resp) closeResponseBody(resp)
return nil, qm, nil return nil, qm, nil
} else if resp.StatusCode != 200 {
closeResponseBody(resp)
return nil, nil, fmt.Errorf("Unexpected response code: %d", resp.StatusCode)
} }
return resp, qm, nil return resp, qm, nil
} }