-
Notifications
You must be signed in to change notification settings - Fork 454
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
[query] Fix incorrect content type in m3query/ error response #2917
Changes from 2 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,30 @@ func WriteError(w http.ResponseWriter, err error, opts ...WriteErrorOption) { | |
fn(&o) | ||
} | ||
|
||
statusCode := getStatusCode(err) | ||
if o.response == nil { | ||
linasm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
return http.StatusGatewayTimeout | ||
} else { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return http.StatusInternalServerError | ||
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: I guess you can avoid both this 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. +1 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. |
||
} | ||
default: | ||
w.WriteHeader(http.StatusInternalServerError) | ||
return http.StatusInternalServerError | ||
} | ||
|
||
if o.response != nil { | ||
w.Write(o.response) | ||
return | ||
} | ||
|
||
json.NewEncoder(w).Encode(ErrorResponse{Error: err.Error()}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make this safer with
value, ok := map[key]
, and also check the length of slice before accessing it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
f858144#diff-391a291f1f91098bc731847f8843207e5327fe87a52a4599a45ac0cf4c5732e7R142-R144