Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loki: Improve error messages on query timeout or cancel #2453

Merged
merged 2 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions pkg/util/server/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ import (
// StatusClientClosedRequest is the status code for when a client request cancellation of an http request
const StatusClientClosedRequest = 499

const (
ErrClientCanceled = "The request was cancelled by the client."
ErrDeadlineExceeded = "Request timed out, decrease the duration of the request or add more label matchers (prefer exact match over regex match) to reduce the amount of data processed."
)

// WriteError write a go error with the correct status code.
func WriteError(err error, w http.ResponseWriter) {
var queryErr chunk.QueryError

switch {
case errors.Is(err, context.Canceled):
http.Error(w, err.Error(), StatusClientClosedRequest)
http.Error(w, ErrClientCanceled, StatusClientClosedRequest)
case errors.Is(err, context.DeadlineExceeded):
http.Error(w, err.Error(), http.StatusGatewayTimeout)
http.Error(w, ErrDeadlineExceeded, http.StatusGatewayTimeout)
case errors.As(err, &queryErr):
http.Error(w, err.Error(), http.StatusBadRequest)
case logql.IsParseError(err):
Expand Down
5 changes: 2 additions & 3 deletions pkg/util/server/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ func Test_writeError(t *testing.T) {
msg string
expectedStatus int
}{
{"cancelled", context.Canceled, context.Canceled.Error(), StatusClientClosedRequest},
{"wrapped cancelled", fmt.Errorf("some context here: %w", context.Canceled), "some context here: " + context.Canceled.Error(), StatusClientClosedRequest},
{"deadline", context.DeadlineExceeded, context.DeadlineExceeded.Error(), http.StatusGatewayTimeout},
{"cancelled", context.Canceled, ErrClientCanceled, StatusClientClosedRequest},
{"deadline", context.DeadlineExceeded, ErrDeadlineExceeded, http.StatusGatewayTimeout},
{"parse error", logql.ParseError{}, "parse error : ", http.StatusBadRequest},
{"httpgrpc", httpgrpc.Errorf(http.StatusBadRequest, errors.New("foo").Error()), "foo", http.StatusBadRequest},
{"internal", errors.New("foo"), "foo", http.StatusInternalServerError},
Expand Down