server: return 500 status on collect errors instead of 200
Errors from provider.Collect() and json.Marshal were returned with HTTP 200, making failures invisible to monitoring, proxies, and clients that check status codes. Return 500 Internal Server Error so HTTP-level tooling can detect failures without parsing the response body.
This commit is contained in:
parent
ef7fffd4b7
commit
2fa44f69a4
2 changed files with 4 additions and 4 deletions
|
|
@ -114,7 +114,7 @@ func (s *Server) handleCollect(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
data, err := s.provider.Collect(r.Context(), req.Options)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, ExternalCollectResponse{
|
||||
writeJSON(w, http.StatusInternalServerError, ExternalCollectResponse{
|
||||
Error: err.Error(),
|
||||
})
|
||||
return
|
||||
|
|
@ -122,7 +122,7 @@ func (s *Server) handleCollect(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
raw, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusOK, ExternalCollectResponse{
|
||||
writeJSON(w, http.StatusInternalServerError, ExternalCollectResponse{
|
||||
Error: fmt.Sprintf("failed to marshal result: %v", err),
|
||||
})
|
||||
return
|
||||
|
|
|
|||
|
|
@ -143,8 +143,8 @@ func TestServer_Collect_ProviderError(t *testing.T) {
|
|||
}
|
||||
srv := newTestServer(p)
|
||||
rec := doRequest(srv.Handler(), "POST", "/collect", ExternalCollectRequest{Key: "test"}, nil)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("POST /collect = %d, want %d", rec.Code, http.StatusOK)
|
||||
if rec.Code != http.StatusInternalServerError {
|
||||
t.Fatalf("POST /collect = %d, want %d", rec.Code, http.StatusInternalServerError)
|
||||
}
|
||||
var resp ExternalCollectResponse
|
||||
json.NewDecoder(rec.Body).Decode(&resp)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue