Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
improve throw
Browse files Browse the repository at this point in the history
  • Loading branch information
wenzuochao authored and peze committed Sep 7, 2020
1 parent 5e64e4f commit 9335d1f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions ChangLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2020-09-07 Version: v1.1.5
- Improve Error.

2020-07-24 Version: v1.1.4
- Support Extra Headers.

Expand Down
20 changes: 18 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,9 @@ func (client *Client) DoRequest(action *string, protocol *string, method *string
res := util.AssertAsMap(obj)
if tea.BoolValue(util.Is4xx(response_.StatusCode)) || tea.BoolValue(util.Is5xx(response_.StatusCode)) {
_err = tea.NewSDKError(map[string]interface{}{
"message": res["Message"],
"code": tea.ToString(DefaultAny(res["Code"], res["code"])) + "Error",
"message": "code: " + tea.ToString(tea.IntValue(response_.StatusCode)) + ", " + tea.ToString(DefaultAny(res["Message"], res["message"])) + " requestid: " + tea.ToString(DefaultAny(res["RequestId"], res["requestId"])),
"data": res,
"code": res["Code"],
})
return _result, _err
}
Expand Down Expand Up @@ -474,3 +474,19 @@ func (client *Client) CheckConfig(config *Config) (_err error) {

return _err
}

/**
* If inputValue is not null, return it or return defaultValue
* @param inputValue users input value
* @param defaultValue default value
* @return the final result
*/
func DefaultAny(inputValue interface{}, defaultValue interface{}) (_result interface{}) {
if tea.BoolValue(util.IsUnset(inputValue)) {
_result = defaultValue
return _result
}

_result = inputValue
return _result
}
4 changes: 2 additions & 2 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ func Test_DoRequest(t *testing.T) {
resp, err := client.DoRequest(tea.String("testApi"), tea.String("HTTP"), tea.String("GET"),
tea.String("2019-12-12"), tea.String("AK"), nil, nil, runtime)
utils.AssertNotNil(t, err)
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: 杭州\n Message: \n Data: {\"Code\":\"杭州\"}\n")
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: 杭州Error\n Message: code: 400, <nil> requestid: <nil>\n Data: {\"Code\":\"杭州\"}\n")
utils.AssertNil(t, resp)

runtime.SetMaxAttempts(3).SetAutoretry(true).SetBackoffPeriod(1).SetBackoffPolicy("ok")
resp, err = client.DoRequest(tea.String("testApi"), tea.String("HTTP"), tea.String("GET"),
tea.String("2019-12-12"), tea.String("AK"), nil, map[string]interface{}{"test": "ok"}, runtime)
utils.AssertNotNil(t, err)
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: 杭州\n Message: \n Data: {\"Code\":\"杭州\"}\n")
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: 杭州Error\n Message: code: 400, <nil> requestid: <nil>\n Data: {\"Code\":\"杭州\"}\n")
utils.AssertNil(t, resp)

ts = mockServer(200, `{"Code": "杭州"}`)
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/alibabacloud-go/tea-rpc
go 1.14

require (
github.com/alibabacloud-go/tea v1.1.7
github.com/alibabacloud-go/tea v1.1.8
github.com/alibabacloud-go/tea-rpc-utils v1.1.1
github.com/alibabacloud-go/tea-utils v1.3.0
github.com/aliyun/credentials-go v1.1.0
github.com/alibabacloud-go/tea-utils v1.3.4
github.com/aliyun/credentials-go v1.1.2
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
)

0 comments on commit 9335d1f

Please sign in to comment.