Skip to content

Commit

Permalink
fix: Add more consistent HTTP errors
Browse files Browse the repository at this point in the history
This commit fixes some status codes that can cause misunderstanding.

Some errors were returning Bad Request (400) even when the user
sends a correct request. The errors originating from the
infrastructure or error during operations should raise
Internal Server Error (500) as they are not expected.

Signed-off-by: Kairo Araujo <[email protected]>
  • Loading branch information
kairoaraujo committed Jan 24, 2024
1 parent aa5d56f commit 0345a9c
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (s *Server) UploadHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
resp, err := s.Upload(r.Context(), r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

Expand Down Expand Up @@ -193,16 +193,21 @@ func (s *Server) DownloadHandler(w http.ResponseWriter, r *http.Request) {
}

vars := mux.Vars(r)
if vars == nil {
http.Error(w, "missing gitoid parameter", http.StatusBadRequest)
return
}

attestationReader, err := s.Download(r.Context(), vars["gitoid"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

defer attestationReader.Close()
if _, err := io.Copy(w, attestationReader); err != nil {
logrus.Errorf("failed to copy attestation to response: %+v", err)
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
return
}

Expand Down

0 comments on commit 0345a9c

Please sign in to comment.