Skip to content

Commit

Permalink
Merge pull request #1813 from influxdb/fix-1813-measurement-not-found
Browse files Browse the repository at this point in the history
Querying for missing measurements should return a 200
  • Loading branch information
pauldix committed Mar 3, 2015
2 parents c201a79 + 294772d commit 97746c9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
13 changes: 13 additions & 0 deletions httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,12 +417,25 @@ func isAuthorizationError(err error) bool {
return ok
}

func isMeasurementNotFoundError(err error) bool {
return (err.Error() == "measurement not found")
}

func isFieldNotFoundError(err error) bool {
return (strings.HasPrefix(err.Error(), "field not found"))
}

// httpResult writes a Results array to the client.
func httpResults(w http.ResponseWriter, results influxdb.Results, pretty bool) {
if results.Error() != nil {
if isAuthorizationError(results.Error()) {
w.WriteHeader(http.StatusUnauthorized)
} else if isMeasurementNotFoundError(results.Error()) {
w.WriteHeader(http.StatusOK)
} else if isFieldNotFoundError(results.Error()) {
w.WriteHeader(http.StatusOK)
} else {
fmt.Println(results.Error())
w.WriteHeader(http.StatusInternalServerError)
}
}
Expand Down
51 changes: 50 additions & 1 deletion httpd/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,55 @@ func TestHandler_DropDatabase_NotFound(t *testing.T) {
}
}

func TestHandler_SelectMeasurement_NotFound(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
s := NewHTTPServer(srvr)
defer s.Close()

query := map[string]string{"q": "CREATE RETENTION POLICY bar ON foo DURATION 1h REPLICATION 1 DEFAULT"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")

if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{}]}` {
t.Fatalf("unexpected body: %s", body)
}

status, body = MustHTTP("GET", s.URL+`/query`, map[string]string{"q": "SELECT value FROM foobarbaz", "db": "foo"}, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"error":"measurement not found"}]}` {
t.Fatalf("unexpected body: %s", body)
}
}

func TestHandler_SelectField_NotFound(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
s := NewHTTPServer(srvr)
defer s.Close()

query := map[string]string{"q": "CREATE RETENTION POLICY bar ON foo DURATION 1h REPLICATION 1 DEFAULT"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")

if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{}]}` {
t.Fatalf("unexpected body: %s", body)
}

// Write some data
_, _ = MustHTTP("POST", s.URL+`/write`, nil, nil, `{"database" : "foo", "retentionPolicy" : "bar", "points": [{"name": "cpu", "tags": {"host": "server01"},"timestamp": "2009-11-10T23:00:00Z","fields": {"value": 100}}]}`)

status, body = MustHTTP("GET", s.URL+`/query`, map[string]string{"q": "SELECT abc FROM cpu", "db": "foo"}, nil, "")
if status != http.StatusOK {
t.Fatalf("unexpected status: %d", status)
} else if body != `{"results":[{"error":"field not found: abc"}]}` {
t.Fatalf("unexpected body: %s", body)
}
}

func TestHandler_RetentionPolicies(t *testing.T) {
srvr := OpenAuthlessServer(NewMessagingClient())
srvr.CreateDatabase("foo")
Expand Down Expand Up @@ -1641,7 +1690,7 @@ func TestHandler_serveWriteSeriesInvalidQueryField(t *testing.T) {

query := map[string]string{"db": "foo", "q": "select bar from cpu"}
status, body := MustHTTP("GET", s.URL+`/query`, query, nil, "")
if status != http.StatusInternalServerError {
if status != http.StatusOK {
t.Logf("query %s\n", query)
t.Log(body)
t.Errorf("unexpected status: %d", status)
Expand Down

0 comments on commit 97746c9

Please sign in to comment.