Skip to content

log detail error message for http error status #6032

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
log detail error message for http error status
  • Loading branch information
half-life666 committed Sep 17, 2024
commit ca2e4d6496064963709a6a55135f7fbdc1aba969
11 changes: 7 additions & 4 deletions weed/server/volume_server_handlers_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ func NotFound(w http.ResponseWriter) {
w.WriteHeader(http.StatusNotFound)
}

func InternalError(w http.ResponseWriter) {
func InternalError(w http.ResponseWriter, err error) {
stats.VolumeServerHandlerCounter.WithLabelValues(stats.ErrorGetInternal).Inc()
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": err.Error(),
})
}

func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -89,7 +92,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
request, err := http.NewRequest(http.MethodGet, r.URL.String(), nil)
if err != nil {
glog.V(0).Infof("failed to instance http request of url %s: %v", r.URL.String(), err)
InternalError(w)
InternalError(w, err)
return
}
for k, vv := range r.Header {
Expand All @@ -101,7 +104,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
response, err := util_http.GetGlobalHttpClient().Do(request)
if err != nil {
glog.V(0).Infof("request remote url %s: %v", r.URL.String(), err)
InternalError(w)
InternalError(w, err)
return
}
defer util_http.CloseResponse(response)
Expand Down Expand Up @@ -165,7 +168,7 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
if err == storage.ErrorNotFound || err == storage.ErrorDeleted {
NotFound(w)
} else {
InternalError(w)
InternalError(w, err)
}
return
}
Expand Down
23 changes: 18 additions & 5 deletions weed/util/http/http_global_client_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/util/mem"
"github.com/seaweedfs/seaweedfs/weed/util"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/util/mem"
)

func Post(url string, values url.Values) ([]byte, error) {
Expand Down Expand Up @@ -312,7 +312,7 @@ func ReadUrlAsStreamAuthenticated(fileUrl, jwt string, cipherKey []byte, isConte
defer CloseResponse(r)
if r.StatusCode >= 400 {
retryable = r.StatusCode == http.StatusNotFound || r.StatusCode >= 499
return retryable, fmt.Errorf("%s: %s", fileUrl, r.Status)
return retryable, fmt.Errorf("%s: %s, detail error: %s", fileUrl, r.Status, getHttpErrorMessage(r))
}

var reader io.ReadCloser
Expand Down Expand Up @@ -392,7 +392,7 @@ func ReadUrlAsReaderCloser(fileUrl string, jwt string, rangeHeader string) (*htt
}
if r.StatusCode >= 400 {
CloseResponse(r)
return nil, nil, fmt.Errorf("%s: %s", fileUrl, r.Status)
return nil, nil, fmt.Errorf("%s: %s, detail error: %s", fileUrl, r.Status, getHttpErrorMessage(r))
}

var reader io.ReadCloser
Expand Down Expand Up @@ -477,4 +477,17 @@ func RetriedFetchChunkData(buffer []byte, urlStrings []string, cipherKey []byte,

return n, err

}
}

func getHttpErrorMessage(resp *http.Response) string {
body, _ := io.ReadAll(resp.Body)

var errMsg string
if len(body) > 0 {
errMsg = string(body)
} else {
errMsg = resp.Status
}

return errMsg
}
Loading