-
Notifications
You must be signed in to change notification settings - Fork 464
[query] Fix incorrect content type in m3query/ error response #2917
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
Changes from 3 commits
d047dfc
15cfbc4
f858144
a63afcc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,25 +92,27 @@ func WriteError(w http.ResponseWriter, err error, opts ...WriteErrorOption) { | |
fn(&o) | ||
} | ||
|
||
statusCode := getStatusCode(err) | ||
if o.response == nil { | ||
w.Header().Set(HeaderContentType, ContentTypeJSON) | ||
w.WriteHeader(statusCode) | ||
Comment on lines
+97
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why in one case it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()}) | ||
} else { | ||
w.WriteHeader(statusCode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: looks like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The order of
It could be done, but either two |
||
w.Write(o.response) | ||
} | ||
} | ||
|
||
func getStatusCode(err error) int { | ||
switch v := err.(type) { | ||
case Error: | ||
w.WriteHeader(v.Code()) | ||
return v.Code() | ||
case error: | ||
if xerrors.IsInvalidParams(v) { | ||
w.WriteHeader(http.StatusBadRequest) | ||
return http.StatusBadRequest | ||
} else if errors.Is(err, context.DeadlineExceeded) { | ||
w.WriteHeader(http.StatusGatewayTimeout) | ||
} else { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return http.StatusGatewayTimeout | ||
} | ||
default: | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
|
||
if o.response != nil { | ||
w.Write(o.response) | ||
return | ||
} | ||
|
||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()}) | ||
return http.StatusInternalServerError | ||
} |
Uh oh!
There was an error while loading. Please reload this page.