From af66437067eafea7c3d588017e975882eeaf2e16 Mon Sep 17 00:00:00 2001 From: nanhe Date: Fri, 10 Jun 2022 15:47:58 +0800 Subject: [PATCH] support statusCode in data for SDKError --- tea/tea.go | 37 ++++++++++++++++++++++++++++--------- tea/tea_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/tea/tea.go b/tea/tea.go index ac5a7e6..4fbddd3 100644 --- a/tea/tea.go +++ b/tea/tea.go @@ -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 } diff --git a/tea/tea_test.go b/tea/tea_test.go index 7ec1dc4..dedb23b 100644 --- a/tea/tea_test.go +++ b/tea/tea_test.go @@ -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) { @@ -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",