Skip to content

Commit

Permalink
support statusCode in data for SDKError
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 authored and peze committed Jun 14, 2022
1 parent e0a739a commit af66437
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 9 deletions.
37 changes: 28 additions & 9 deletions tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,22 +178,41 @@ 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))
}
if data := obj["data"]; data != nil {
r := reflect.ValueOf(data)
if r.Kind().String() == "map" {
res := make(map[string]interface{})
tmp := r.MapKeys()
for _, key := range tmp {
res[key.String()] = r.MapIndex(key).Interface()
}
if statusCode := res["statusCode"]; statusCode != nil {
if code, ok := statusCode.(int); ok {
err.StatusCode = Int(code)
} else if tmp, ok := statusCode.(string); ok {
code, err_ := strconv.Atoi(tmp)
if err_ == nil {
err.StatusCode = Int(code)
}
}
}
}
byt, _ := json.Marshal(data)
err.Data = String(string(byt))
}

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

return err
}

Expand Down
47 changes: 47 additions & 0 deletions tea/tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,51 @@ func TestSDKError(t *testing.T) {

err.SetErrMsg("test")
utils.AssertEqual(t, "test", err.Error())
utils.AssertEqual(t, 404, *err.StatusCode)

err = NewSDKError(map[string]interface{}{
"statusCode": "404",
"data": map[string]interface{}{
"statusCode": 500,
},
})
utils.AssertNotNil(t, err)
utils.AssertEqual(t, 404, *err.StatusCode)

err = NewSDKError(map[string]interface{}{
"data": map[string]interface{}{
"statusCode": 500,
},
})
utils.AssertNotNil(t, err)
utils.AssertEqual(t, 500, *err.StatusCode)

err = NewSDKError(map[string]interface{}{
"data": map[string]interface{}{
"statusCode": "500",
},
})
utils.AssertNotNil(t, err)
utils.AssertEqual(t, 500, *err.StatusCode)

err = NewSDKError(map[string]interface{}{
"code": "code",
"message": "message",
"data": map[string]interface{}{
"requestId": "dfadfa32cgfdcasd4313",
},
})
utils.AssertNotNil(t, err)
utils.AssertNil(t, err.StatusCode)

err = NewSDKError(map[string]interface{}{
"code": "code",
"message": "message",
"data": "string data",
})
utils.AssertNotNil(t, err)
utils.AssertNotNil(t, err.Data)
utils.AssertNil(t, err.StatusCode)
}

func TestSDKErrorCode404(t *testing.T) {
Expand Down Expand Up @@ -252,9 +297,11 @@ type Test struct {
func TestToMap(t *testing.T) {
in := map[string]*string{
"tea": String("test"),
"nil": nil,
}
result := ToMap(in)
utils.AssertEqual(t, "test", result["tea"])
utils.AssertNil(t, result["nil"])

validMap := map[string]interface{}{
"valid": "test",
Expand Down

0 comments on commit af66437

Please sign in to comment.