Skip to content

Commit

Permalink
fix: enhance content type for head request
Browse files Browse the repository at this point in the history
  • Loading branch information
martinconic committed Nov 27, 2024
1 parent dca6b30 commit b4164ff
Showing 1 changed file with 2 additions and 17 deletions.
19 changes: 2 additions & 17 deletions pkg/api/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package api

import (
"encoding/binary"
"encoding/json"
"errors"
"fmt"
"net/http"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/ethersphere/bee/v2/pkg/cac"
"github.com/ethersphere/bee/v2/pkg/file/redundancy"
"github.com/ethersphere/bee/v2/pkg/jsonhttp"
"github.com/ethersphere/bee/v2/pkg/log"
"github.com/ethersphere/bee/v2/pkg/postage"
"github.com/ethersphere/bee/v2/pkg/storage"
"github.com/ethersphere/bee/v2/pkg/swarm"
Expand Down Expand Up @@ -194,7 +192,7 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) {
Address swarm.Address `map:"address,resolve" validate:"required"`
}{}
if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
respondWithError(w, http.StatusBadRequest, "Invalid path parameters", "Ensure address is provided", logger)
w.WriteHeader(http.StatusBadRequest)
return
}

Expand All @@ -208,7 +206,7 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) {
if err != nil {
logger.Debug("get root chunk failed", "chunk_address", address, "error", err)
logger.Error(nil, "get root chunk failed")
respondWithError(w, http.StatusNotFound, "Chunk not found", "The requested chunk could not be retrieved", logger)
w.WriteHeader(http.StatusNotFound)
return
}

Expand All @@ -224,16 +222,3 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set(ContentLengthHeader, strconv.FormatInt(span, 10))
w.WriteHeader(http.StatusOK) // HEAD requests do not write a body
}

func respondWithError(w http.ResponseWriter, status int, title, detail string, logger log.Logger) {
w.Header().Set("Content-Type", "application/problem+json")
w.WriteHeader(status)
errorResponse := map[string]interface{}{
"type": "about:blank", // Or a URI if you have one
"title": title,
"status": status,
"detail": detail,
}
logger.Error(nil, title) // Optionally log the error
json.NewEncoder(w).Encode(errorResponse)
}

0 comments on commit b4164ff

Please sign in to comment.