Compare commits

..

1 Commits

Author SHA1 Message Date
nanhe b9ace42a5b feat: return description and accessDeniedDetail in error info 2022-10-20 15:56:55 +08:00
2 changed files with 33 additions and 6 deletions
+22 -6
View File
@@ -69,12 +69,14 @@ type Response struct {
// SDKError struct is used save error code and message
type SDKError struct {
Code *string
StatusCode *int
Message *string
Data *string
Stack *string
errMsg *string
Code *string
StatusCode *int
Message *string
Data *string
Stack *string
errMsg *string
Description *string
AccessDeniedDetail map[string]interface{}
}
// RuntimeObject is used for converting http configuration
@@ -181,6 +183,20 @@ func NewSDKError(obj map[string]interface{}) *SDKError {
if obj["message"] != nil {
err.Message = String(obj["message"].(string))
}
if obj["description"] != nil {
err.Description = String(obj["description"].(string))
}
if detail := obj["accessDeniedDetail"]; detail != nil {
r := reflect.ValueOf(detail)
if r.Kind().String() == "map" {
res := make(map[string]interface{})
tmp := r.MapKeys()
for _, key := range tmp {
res[key.String()] = r.MapIndex(key).Interface()
}
err.AccessDeniedDetail = res
}
}
if data := obj["data"]; data != nil {
r := reflect.ValueOf(data)
if r.Kind().String() == "map" {
+11
View File
@@ -169,6 +169,14 @@ func TestSDKError(t *testing.T) {
"requestId": "dfadfa32cgfdcasd4313",
"hostId": "github.com/alibabacloud/tea",
},
"description": "description",
"accessDeniedDetail": map[string]interface{}{
"AuthAction": "ram:ListUsers",
"AuthPrincipalType": "SubUser",
"PolicyType": "ResourceGroupLevelIdentityBassdPolicy",
"NoPermissionType": "ImplicitDeny",
"UserId": 123,
},
})
utils.AssertNotNil(t, err)
utils.AssertEqual(t, "SDKError:\n StatusCode: 404\n Code: code\n Message: message\n Data: {\"hostId\":\"github.com/alibabacloud/tea\",\"httpCode\":\"404\",\"requestId\":\"dfadfa32cgfdcasd4313\"}\n", err.Error())
@@ -176,6 +184,9 @@ func TestSDKError(t *testing.T) {
err.SetErrMsg("test")
utils.AssertEqual(t, "test", err.Error())
utils.AssertEqual(t, 404, *err.StatusCode)
utils.AssertEqual(t, "description", *err.Description)
utils.AssertEqual(t, "ImplicitDeny", err.AccessDeniedDetail["NoPermissionType"])
utils.AssertEqual(t, 123, err.AccessDeniedDetail["UserId"])
err = NewSDKError(map[string]interface{}{
"statusCode": "404",