Browse Source

log detail error message for http error status

pull/6032/head
half-life666 3 months ago
parent
commit
ca2e4d6496
  1. 11
      weed/server/volume_server_handlers_read.go
  2. 23
      weed/util/http/http_global_client_util.go

11
weed/server/volume_server_handlers_read.go

@ -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) {
@ -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 {
@ -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)
@ -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
}

23
weed/util/http/http_global_client_util.go

@ -5,8 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/seaweedfs/seaweedfs/weed/util/mem"
"github.com/seaweedfs/seaweedfs/weed/util"
"io"
"net/http"
"net/url"
@ -14,6 +12,8 @@ import (
"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) {
@ -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
@ -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
@ -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…
Cancel
Save