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

Commit

Permalink
add httpcode (#2)
Browse files Browse the repository at this point in the history
* add http status code

Co-authored-by: zigang.wang <[email protected]>
  • Loading branch information
ziggyw and zigang.wang authored Jul 23, 2021
1 parent bcf4222 commit ae1cf12
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
7 changes: 4 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,10 @@ 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{}{
"code": tea.ToString(DefaultAny(res["Code"], res["code"])),
"message": "code: " + tea.ToString(tea.IntValue(response_.StatusCode)) + ", " + tea.ToString(DefaultAny(res["Message"], res["message"])) + " request id: " + tea.ToString(DefaultAny(res["RequestId"], res["requestId"])),
"data": res,
"code": tea.ToString(DefaultAny(res["Code"], res["code"])),
"statusCode": tea.IntValue(response_.StatusCode),
"message": "code: " + tea.ToString(tea.IntValue(response_.StatusCode)) + ", " + tea.ToString(DefaultAny(res["Message"], res["message"])) + " request id: " + tea.ToString(DefaultAny(res["RequestId"], res["requestId"])),
"data": res,
})
return _result, _err
}
Expand Down
10 changes: 5 additions & 5 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func Test_DoRequest(t *testing.T) {

_, err = NewClient(nil)
utils.AssertNotNil(t, err)
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: ParameterMissing\n Message: 'config' can not be unset\n Data: \n")
utils.AssertEqual(t, err.Error(), "SDKError:\n StatusCode: 0\n Code: ParameterMissing\n Message: 'config' can not be unset\n Data: \n")

config.SetRegionId("cn-hangzhou")
_, err = NewClient(config)
utils.AssertNotNil(t, err)
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: ParameterMissing\n Message: 'accessKeyId' and 'accessKeySecret' or 'credential' can not be unset\n Data: \n")
utils.AssertEqual(t, err.Error(), "SDKError:\n StatusCode: 0\n Code: ParameterMissing\n Message: 'accessKeyId' and 'accessKeySecret' or 'credential' can not be unset\n Data: \n")

config.SetCredential(c)
_, err = NewClient(config)
Expand Down 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: code: 400, <nil> request id: <nil>\n Data: {\"Code\":\"杭州\"}\n")
utils.AssertEqual(t, err.Error(), "SDKError:\n StatusCode: 400\n Code: 杭州\n Message: code: 400, <nil> request id: <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: code: 400, <nil> request id: <nil>\n Data: {\"Code\":\"杭州\"}\n")
utils.AssertEqual(t, err.Error(), "SDKError:\n StatusCode: 400\n Code: 杭州\n Message: code: 400, <nil> request id: <nil>\n Data: {\"Code\":\"杭州\"}\n")
utils.AssertNil(t, resp)

ts = mockServer(200, `{"Code": "杭州"}`)
Expand Down Expand Up @@ -128,5 +128,5 @@ func Test_DoRequest(t *testing.T) {
config.SetEndpoint("")
client.EndpointRule = tea.String("")
err = client.CheckConfig(config)
utils.AssertEqual(t, err.Error(), "SDKError:\n Code: ParameterMissing\n Message: 'config.endpoint' can not be empty\n Data: \n")
utils.AssertEqual(t, err.Error(), "SDKError:\n StatusCode: 0\n Code: ParameterMissing\n Message: 'config.endpoint' can not be empty\n Data: \n")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/alibabacloud-go/tea-rpc
go 1.14

require (
github.com/alibabacloud-go/tea v1.1.10
github.com/alibabacloud-go/tea v1.1.17
github.com/alibabacloud-go/tea-rpc-utils v1.1.2
github.com/alibabacloud-go/tea-utils v1.3.5
github.com/aliyun/credentials-go v1.1.2
Expand Down

0 comments on commit ae1cf12

Please sign in to comment.