Compare commits

...

3 Commits

Author SHA1 Message Date
nanhe 3a75a83be1 support when statusCode is *int 2022-06-20 20:19:19 +08:00
nanhe af66437067 support statusCode in data for SDKError 2022-06-14 16:16:13 +08:00
nanhe e0a739a681 add Action 2022-06-14 16:16:13 +08:00
3 changed files with 120 additions and 9 deletions
+35
View File
@@ -0,0 +1,35 @@
name: Go CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
go: [1.13, 1.14, 1.15]
steps:
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Check out code into the Go module directory
uses: actions/checkout@v2
- name: Build Tea
run: go build ./tea
- name: Build Util
run: go build ./utils
- name: Test
run: go test -race -coverprofile=coverage.txt -covermode=atomic ./tea/... ./utils/...
- name: CodeCov
run: bash <(curl -s https://codecov.io/bash)
+30 -9
View File
@@ -178,22 +178,43 @@ 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)
}
} else if code, ok := statusCode.(*int); ok {
err.StatusCode = 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
}
+55
View File
@@ -175,6 +175,59 @@ 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": Int(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 +305,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",