-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standardize error return from HTTP requests (#363)
This makes the HTTP request errors `As`-able, so you can retry on rate limit errors, or whatever. Fixes #333, replaces #352 (I just added docs). I have: - [x] Written a clear PR title and description (above) - [x] Signed the [Khan Academy CLA](https://www.khanacademy.org/r/cla) - [x] Added tests covering my changes, if applicable - [x] Included a link to the issue fixed, if applicable - [x] Included documentation, for new features - [x] Added an entry to the changelog --------- Co-authored-by: William Zeng <[email protected]>
- Loading branch information
1 parent
ca5889f
commit adb9dd6
Showing
6 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package graphql | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMakeRequest_HTTPError(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
serverResponseBody string | ||
expectedErrorBody string | ||
serverResponseCode int | ||
expectedStatusCode int | ||
}{ | ||
{ | ||
name: "400 Bad Request", | ||
serverResponseBody: "Bad Request", | ||
expectedErrorBody: "Bad Request", | ||
serverResponseCode: http.StatusBadRequest, | ||
expectedStatusCode: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "429 Too Many Requests", | ||
serverResponseBody: "Rate limit exceeded", | ||
expectedErrorBody: "Rate limit exceeded", | ||
serverResponseCode: http.StatusTooManyRequests, | ||
expectedStatusCode: http.StatusTooManyRequests, | ||
}, | ||
{ | ||
name: "500 Internal Server Error", | ||
serverResponseBody: "Internal Server Error", | ||
expectedErrorBody: "Internal Server Error", | ||
serverResponseCode: http.StatusInternalServerError, | ||
expectedStatusCode: http.StatusInternalServerError, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(tc.serverResponseCode) | ||
_, err := w.Write([]byte(tc.serverResponseBody)) | ||
if err != nil { | ||
t.Fatalf("Failed to write response: %v", err) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
client := NewClient(server.URL, server.Client()) | ||
req := &Request{ | ||
Query: "query { test }", | ||
} | ||
resp := &Response{} | ||
|
||
err := client.MakeRequest(context.Background(), req, resp) | ||
|
||
assert.Error(t, err) | ||
var httpErr *HTTPError | ||
assert.True(t, errors.As(err, &httpErr), "Error should be of type *HTTPError") | ||
assert.Equal(t, tc.expectedStatusCode, httpErr.StatusCode) | ||
assert.Equal(t, tc.expectedErrorBody, httpErr.Body) | ||
}) | ||
} | ||
} | ||
|
||
func TestMakeRequest_Success(t *testing.T) { | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
err := json.NewEncoder(w).Encode(map[string]interface{}{ | ||
"data": map[string]string{ | ||
"test": "success", | ||
}, | ||
}) | ||
if err != nil { | ||
t.Fatalf("Failed to encode response: %v", err) | ||
} | ||
})) | ||
defer server.Close() | ||
|
||
client := NewClient(server.URL, server.Client()) | ||
req := &Request{ | ||
Query: "query { test }", | ||
} | ||
resp := &Response{} | ||
|
||
err := client.MakeRequest(context.Background(), req, resp) | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, map[string]interface{}{"test": "success"}, resp.Data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package graphql | ||
|
||
import "fmt" | ||
|
||
// HTTPError represents an HTTP error with status code and response body. | ||
type HTTPError struct { | ||
Body string | ||
StatusCode int | ||
} | ||
|
||
// Error implements the error interface for HTTPError. | ||
func (e *HTTPError) Error() string { | ||
return fmt.Sprintf("returned error %v: %s", e.StatusCode, e.Body) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters