Skip to content

Commit

Permalink
add StatusCode for SDK Error
Browse files Browse the repository at this point in the history
Co-authored-by: zigang.wang <zigang.wang@alibaba-inc.com>
  • Loading branch information
ziggyw and zigang.wang authored Jul 23, 2021
1 parent fc13b6e commit d9ff2bf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 21 deletions.
34 changes: 22 additions & 12 deletions tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,12 @@ type Response struct {

// SDKError struct is used save error code and message
type SDKError struct {
Code *string
Message *string
Data *string
Stack *string
errMsg *string
Code *string
StatusCode *int
Message *string
Data *string
Stack *string
errMsg *string
}

// RuntimeObject is used for converting http configuration
Expand Down Expand Up @@ -177,6 +178,15 @@ func NewSDKError(obj map[string]interface{}) *SDKError {
err.Code = String(val)
}

if statusCode, ok := obj["statusCode"].(int); ok {
err.StatusCode = Int(statusCode)
} else if status, ok := obj["statusCode"].(string); ok {
statusCode, err2 := strconv.Atoi(status)
if err2 == nil {
err.StatusCode = Int(statusCode)
}
}

if obj["message"] != nil {
err.Message = String(obj["message"].(string))
}
Expand All @@ -194,8 +204,8 @@ func (err *SDKError) SetErrMsg(msg string) {

func (err *SDKError) Error() string {
if err.errMsg == nil {
str := fmt.Sprintf("SDKError:\n Code: %s\n Message: %s\n Data: %s\n",
StringValue(err.Code), StringValue(err.Message), StringValue(err.Data))
str := fmt.Sprintf("SDKError:\n StatusCode: %d\n Code: %s\n Message: %s\n Data: %s\n",
IntValue(err.StatusCode), StringValue(err.Code), StringValue(err.Message), StringValue(err.Data))
err.SetErrMsg(str)
}
return StringValue(err.errMsg)
Expand All @@ -209,8 +219,8 @@ func (err *CastError) Error() string {
// Convert is use convert map[string]interface object to struct
func Convert(in interface{}, out interface{}) error {
byt, _ := json.Marshal(in)
decoder := jsonParser.NewDecoder(bytes.NewReader(byt))
decoder.UseNumber();
decoder := jsonParser.NewDecoder(bytes.NewReader(byt))
decoder.UseNumber()
err := decoder.Decode(&out)
return err
}
Expand Down Expand Up @@ -767,10 +777,10 @@ func Retryable(err error) *bool {
return Bool(false)
}
if realErr, ok := err.(*SDKError); ok {
code, err := strconv.Atoi(StringValue(realErr.Code))
if err != nil {
return Bool(true)
if realErr.StatusCode == nil {
return Bool(false)
}
code := IntValue(realErr.StatusCode)
return Bool(code >= http.StatusInternalServerError)
}
return Bool(true)
Expand Down
40 changes: 31 additions & 9 deletions tea/tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ func TestConvert(t *testing.T) {
utils.AssertEqual(t, "test", string(out.Body))
}

func TestConvertType(t *testing.T){
func TestConvertType(t *testing.T) {
in := map[string]interface{}{
"key": 123,
"key": 123,
"body": []byte("test"),
}
out := new(test)
Expand All @@ -161,28 +161,30 @@ func TestRuntimeObject(t *testing.T) {

func TestSDKError(t *testing.T) {
err := NewSDKError(map[string]interface{}{
"code": "code",
"message": "message",
"code": "code",
"statusCode": 404,
"message": "message",
"data": map[string]interface{}{
"httpCode": "404",
"requestId": "dfadfa32cgfdcasd4313",
"hostId": "github.com/alibabacloud/tea",
},
})
utils.AssertNotNil(t, err)
utils.AssertEqual(t, "SDKError:\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error())
utils.AssertEqual(t, "SDKError:\n StatusCode: 404\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error())

err.SetErrMsg("test")
utils.AssertEqual(t, "test", err.Error())
}

func TestSDKErrorCode404(t *testing.T) {
err := NewSDKError(map[string]interface{}{
"code": 404,
"message": "message",
"statusCode": 404,
"code": "NOTFOUND",
"message": "message",
})
utils.AssertNotNil(t, err)
utils.AssertEqual(t, "SDKError:\n Code: 404\n Message: message\n Data: \n", err.Error())
utils.AssertEqual(t, "SDKError:\n StatusCode: 404\n Code: NOTFOUND\n Message: message\n Data: \n", err.Error())
}

func TestToObject(t *testing.T) {
Expand Down Expand Up @@ -334,9 +336,29 @@ func Test_Retryable(t *testing.T) {
}
err = NewSDKError(errmsg)
ifRetry = Retryable(err)
utils.AssertEqual(t, false, BoolValue(ifRetry))

errmsg["statusCode"] = 400
err = NewSDKError(errmsg)
ifRetry = Retryable(err)
utils.AssertEqual(t, false, BoolValue(ifRetry))

errmsg["statusCode"] = "400"
err = NewSDKError(errmsg)
ifRetry = Retryable(err)
utils.AssertEqual(t, false, BoolValue(ifRetry))

errmsg["statusCode"] = 500
err = NewSDKError(errmsg)
ifRetry = Retryable(err)
utils.AssertEqual(t, true, BoolValue(ifRetry))

errmsg["statusCode"] = "500"
err = NewSDKError(errmsg)
ifRetry = Retryable(err)
utils.AssertEqual(t, true, BoolValue(ifRetry))

errmsg["code"] = "400"
errmsg["statusCode"] = "test"
err = NewSDKError(errmsg)
ifRetry = Retryable(err)
utils.AssertEqual(t, false, BoolValue(ifRetry))
Expand Down

0 comments on commit d9ff2bf

Please sign in to comment.