Skip to content

Commit 5e12c9d

Browse files
authored
SYN-5006: Expose status code for api errors (#31)
1 parent c503e10 commit 5e12c9d

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

syntheticsclientv2/synthetics.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ func (c Client) makePublicAPICall(method string, endpoint string, requestBody io
9696
return &details, err
9797
}
9898

99+
details.StatusCode = resp.StatusCode
100+
details.RawResponse = resp
101+
99102
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
100103
var errRes errorResponse
101104
if err = json.NewDecoder(resp.Body).Decode(&errRes); err == nil {
@@ -113,9 +116,7 @@ func (c Client) makePublicAPICall(method string, endpoint string, requestBody io
113116
return &details, err
114117
}
115118

116-
details.StatusCode = resp.StatusCode
117119
details.ResponseBody = string(responseBody)
118-
details.RawResponse = resp
119120

120121
return &details, nil
121122
}

syntheticsclientv2/synthetics_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,26 @@ func TestConfigurableClientTimeout(t *testing.T) {
9898
t.Errorf("expected to see timeout error, but saw: %s", err.Error())
9999
}
100100
}
101+
102+
func TestConfigurableClientErrorStatusCode(t *testing.T) {
103+
testMux = http.NewServeMux()
104+
testServer = httptest.NewServer(testMux)
105+
106+
testMux.HandleFunc("/tests", func(w http.ResponseWriter, r *http.Request) {
107+
w.WriteHeader(http.StatusNotFound)
108+
w.Write([]byte(`{"status":"404"}`))
109+
})
110+
111+
testConfigurableClient := NewConfigurableClient("apiKey", "realm", ClientArgs{
112+
publicBaseUrl: testServer.URL,
113+
})
114+
details, err := testConfigurableClient.makePublicAPICall("GET", "/tests", nil, nil)
115+
116+
if !strings.Contains(err.Error(), "404 Not Found") {
117+
t.Errorf("expected to see 404 error, but saw: %s", err.Error())
118+
}
119+
120+
if details.StatusCode != http.StatusNotFound {
121+
t.Errorf("expected to see 404 status code, but saw: %d", details.StatusCode)
122+
}
123+
}

0 commit comments

Comments
 (0)