Skip to content

Commit

Permalink
added a better error message if the query is malformed
Browse files Browse the repository at this point in the history
  • Loading branch information
danielamkaer committed Mar 19, 2020
1 parent d3e6e14 commit ecc29c6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
19 changes: 18 additions & 1 deletion api/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -48,6 +49,14 @@ type QueryResult struct {
Metadata QueryResultMetadata `json:"metaData"`
}

type QueryError struct {
error string
}

func (e QueryError) Error() string {
return e.error
}

func (q QueryJobs) Create(repository string, query Query) (string, error) {
var buf bytes.Buffer
err := json.NewEncoder(&buf).Encode(query)
Expand All @@ -62,7 +71,15 @@ func (q QueryJobs) Create(repository string, query Query) (string, error) {
return "", err
}

if resp.StatusCode != http.StatusOK {
switch resp.StatusCode {
case http.StatusBadRequest:
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return "", QueryError{string(body)}
case http.StatusOK:
default:
return "", fmt.Errorf("could not create query job, got status code %d", resp.StatusCode)
}

Expand Down
15 changes: 10 additions & 5 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ func newSearchCmd() *cobra.Command {

// run in lambda func to be able to defer and delete the query job
err := func() error {
var progress *queryResultProgressBar
if !noProgress {
progress = newQueryResultProgressBar()
}

id, err := client.QueryJobs().Create(repository, api.Query{
QueryString: queryString,
Start: start,
Expand All @@ -57,6 +52,11 @@ func newSearchCmd() *cobra.Command {
return err
}

var progress *queryResultProgressBar
if !noProgress {
progress = newQueryResultProgressBar()
}

defer func(id string) {
// Humio will eventually delete the query when we stop polling and we can't do much about errors here.
_ = client.QueryJobs().Delete(repository, id)
Expand Down Expand Up @@ -119,6 +119,11 @@ func newSearchCmd() *cobra.Command {
err = nil
}

if queryError, ok := err.(api.QueryError); ok {
fmt.Printf("There was an error in your query string:\n\n%s\n", queryError.Error())
os.Exit(1)
}

exitOnError(cmd, err, "error running search")
},
}
Expand Down

0 comments on commit ecc29c6

Please sign in to comment.