From ca2e4d6496064963709a6a55135f7fbdc1aba969 Mon Sep 17 00:00:00 2001 From: half-life666 Date: Tue, 17 Sep 2024 17:14:28 +0800 Subject: [PATCH] log detail error message for http error status --- weed/server/volume_server_handlers_read.go | 11 +++++++---- weed/util/http/http_global_client_util.go | 23 +++++++++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go index 15d639f49..8e371e2da 100644 --- a/weed/server/volume_server_handlers_read.go +++ b/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 } diff --git a/weed/util/http/http_global_client_util.go b/weed/util/http/http_global_client_util.go index c3931a790..6d0fe1304 100644 --- a/weed/util/http/http_global_client_util.go +++ b/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 -} \ No newline at end of file +} + +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 +}