Skip to content

Commit 38bb2c6

Browse files
Vitess: handle non-200 HTTP response
1 parent fb0040a commit 38bb2c6

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pkg/vitess/api_client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func filterReplicaTablets(settings config.VitessConfigurationSettings, tablets [
7171
return replicas
7272
}
7373

74-
// ParseTablets reads from vitess /api/ks_tablets/<keyspace>/[shard] and returns a
74+
// ParseTablets reads from vitess /api/keyspace/<keyspace>/tablets/[shard] and returns a
7575
// listing (mysql_hostname, mysql_port, type) of REPLICA tablets
7676
func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet, err error) {
7777
if settings.TimeoutSecs == 0 {
@@ -85,13 +85,16 @@ func ParseTablets(settings config.VitessConfigurationSettings) (tablets []Tablet
8585
if err != nil {
8686
return tablets, err
8787
}
88-
8988
defer resp.Body.Close()
89+
90+
if resp.StatusCode != http.StatusOK {
91+
return tablets, fmt.Errorf("%v", resp.Status)
92+
}
93+
9094
body, err := ioutil.ReadAll(resp.Body)
9195
if err != nil {
9296
return tablets, err
9397
}
94-
9598
err = json.Unmarshal(body, &tablets)
9699
return filterReplicaTablets(settings, tablets), err
97100
}

pkg/vitess/api_client_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func TestParseTablets(t *testing.T) {
5656
fmt.Fprint(w, string(data))
5757
default:
5858
w.WriteHeader(http.StatusNotFound)
59-
fmt.Fprint(w, "[]")
6059
}
6160
}))
6261
defer vitessApi.Close()
@@ -125,11 +124,11 @@ func TestParseTablets(t *testing.T) {
125124
Keyspace: "not-found",
126125
Shard: "40-80",
127126
})
128-
if err != nil {
129-
t.Fatalf("Expected no error, got %q", err)
127+
if err == nil || err.Error() != "404 Not Found" {
128+
t.Fatalf("Expected %q error, got %q", "404 Not Found", err)
130129
}
131130

132-
if len(tablets) > 0 {
131+
if len(tablets) != 0 {
133132
t.Fatalf("Expected 0 tablets, got %d", len(tablets))
134133
}
135134

0 commit comments

Comments
 (0)