Skip to content

Commit

Permalink
Merge pull request #958 from lindseysimple/issue-954-2
Browse files Browse the repository at this point in the history
fix: Replace the error message with BaseResponse.Message
  • Loading branch information
cloudxxx8 authored Nov 29, 2024
2 parents b7711f3 + 89e02a4 commit 51b22cb
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion clients/http/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 51b22cb

Please sign in to comment.