Introduce APIError type and make ErrorEvent.Err public

This makes it a little bit easier to act on API errors that happen while
streaming.
This commit is contained in:
Alexander Bakker 2023-03-11 12:10:46 +01:00
parent 9faaa4f0dc
commit 972ffb4771
6 changed files with 49 additions and 24 deletions

View file

@ -73,7 +73,11 @@ func TestString(t *testing.T) {
func TestParseAPIError(t *testing.T) {
// No api error.
r := ioutil.NopCloser(strings.NewReader(`<html><head><title>404</title></head></html>`))
err := parseAPIError("bad request", &http.Response{Status: "404 Not Found", Body: r})
err := parseAPIError("bad request", &http.Response{
Status: "404 Not Found",
StatusCode: http.StatusNotFound,
Body: r,
})
want := "bad request: 404 Not Found"
if err.Error() != want {
t.Fatalf("want %q but %q", want, err.Error())
@ -81,7 +85,11 @@ func TestParseAPIError(t *testing.T) {
// With api error.
r = ioutil.NopCloser(strings.NewReader(`{"error":"Record not found"}`))
err = parseAPIError("bad request", &http.Response{Status: "404 Not Found", Body: r})
err = parseAPIError("bad request", &http.Response{
Status: "404 Not Found",
StatusCode: http.StatusNotFound,
Body: r,
})
want = "bad request: 404 Not Found: Record not found"
if err.Error() != want {
t.Fatalf("want %q but %q", want, err.Error())