From 89e02a4eecadce9d454acc7ea4305d261c7934c0 Mon Sep 17 00:00:00 2001 From: Lindsey Cheng Date: Fri, 29 Nov 2024 11:31:56 +0800 Subject: [PATCH] fix: Replace the error message with BaseResponse.Message Replace the error message with BaseResponse.Message instead of the whole BaseResponse string. Signed-off-by: Lindsey Cheng --- clients/http/utils/common.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/clients/http/utils/common.go b/clients/http/utils/common.go index d1815d05..2fac519b 100644 --- a/clients/http/utils/common.go +++ b/clients/http/utils/common.go @@ -21,6 +21,7 @@ import ( "github.com/edgexfoundry/go-mod-core-contracts/v4/clients/interfaces" "github.com/edgexfoundry/go-mod-core-contracts/v4/common" + dtosCommon "github.com/edgexfoundry/go-mod-core-contracts/v4/dtos/common" "github.com/edgexfoundry/go-mod-core-contracts/v4/errors" "github.com/google/uuid" @@ -231,8 +232,19 @@ func SendRequest(ctx context.Context, req *http.Request, authInjector interfaces return bodyBytes, nil } + var errMsg string + var errResp dtosCommon.BaseResponse + // If the bodyBytes can be unmarshalled to BaseResponse DTO, use the BaseResponse.Message field as the error message + // Otherwise, use the whole bodyBytes string as the error message + baseRespErr := json.Unmarshal(bodyBytes, &errResp) + if baseRespErr == nil { + errMsg = errResp.Message + } else { + errMsg = string(bodyBytes) + } + // Handle error response - msg := fmt.Sprintf("request failed, status code: %d, err: %s", resp.StatusCode, string(bodyBytes)) + msg := fmt.Sprintf("request failed, status code: %d, err: %s", resp.StatusCode, errMsg) errKind := errors.KindMapping(resp.StatusCode) return bodyBytes, errors.NewCommonEdgeX(errKind, msg, nil) }