Skip to content

Commit

Permalink
Merge pull request #89 from capeprivacy/eric/add-nil-check
Browse files Browse the repository at this point in the history
add error check around response handling
  • Loading branch information
gavinuhma authored Jul 14, 2022
2 parents d3ae06d + 80e9d9b commit 02f21bb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
15 changes: 10 additions & 5 deletions capetest/capetest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package capetest
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -52,14 +53,18 @@ func websocketDial(url string, insecure bool) (*websocket.Conn, *http.Response,

func CapeTest(testReq TestRequest, endpoint string, insecure bool) (*RunResults, error) {
conn, resp, err := websocketDial(endpoint, insecure)
defer resp.Body.Close()
if err != nil {
log.Error("error dialing websocket", err)
var e ErrorMsg
if err := json.NewDecoder(resp.Body).Decode(&e); err != nil {
return nil, err
// This check is necessary because we don't necessarily return an http response from sentinel.
// Http error code and message is returned if network routing fails.
if resp != nil {
var e ErrorMsg
if err := json.NewDecoder(resp.Body).Decode(&e); err != nil {
return nil, err
}
resp.Body.Close()
return nil, fmt.Errorf("error code: %d, reason: %s", resp.StatusCode, e.Error)
}
log.Errorf("error code: %d, reason: %s", resp.StatusCode, e.Error)
return nil, err
}

Expand Down
14 changes: 9 additions & 5 deletions cmd/cape/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,18 @@ func doDeploy(url string, name string, reader io.Reader, insecure bool) (string,
s.Start()

conn, res, err := websocketDial(endpoint, insecure)
defer res.Body.Close()
if err != nil {
log.Error("error dialing websocket", err)
var e ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return "", err
// This check is necessary because we don't necessarily return an http response from sentinel.
// Http error code and message is returned if network routing fails.
if res != nil {
var e ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return "", err
}
res.Body.Close()
return "", fmt.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error)
}
log.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error)
return "", err
}

Expand Down
23 changes: 9 additions & 14 deletions cmd/cape/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,18 @@ func doRun(url string, functionID string, data []byte, insecure bool) ([]byte, e
endpoint := fmt.Sprintf("%s/v1/run/%s", url, functionID)

c, res, err := websocketDial(endpoint, insecure)
defer res.Body.Close()
if err != nil {
log.Error("error dialing websocket", err)
var e ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return nil, err
// This check is necessary because we don't necessarily return an http response from sentinel.
// Http error code and message is returned if network routing fails.
if res != nil {
var e ErrorMsg
if err := json.NewDecoder(res.Body).Decode(&e); err != nil {
return nil, err
}
res.Body.Close()
return nil, fmt.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error)
}
log.Errorf("error code: %d, reason: %s", res.StatusCode, e.Error)
return nil, err
}
nonce, err := crypto.GetNonce()
Expand All @@ -140,15 +144,6 @@ func doRun(url string, functionID string, data []byte, insecure bool) ([]byte, e
log.Println("error writing deploy request")
return nil, err
}
// Try to read message
t, socketMsg, err := c.ReadMessage()
if err != nil {
log.Error("failed to fetch response", err)
}

if t == websocket.CloseMessage {
log.Errorf("failed to run with: %s", string(socketMsg))
}

var msg Message
err = c.ReadJSON(&msg)
Expand Down

0 comments on commit 02f21bb

Please sign in to comment.