Skip to content

Commit

Permalink
feat: return description and accessDeniedDetail in error info
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Oct 20, 2022
1 parent 3a75a83 commit b9ace42
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
28 changes: 22 additions & 6 deletions tea/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" {
Expand Down
11 changes: 11 additions & 0 deletions tea/tea_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,24 @@ 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())

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",
Expand Down

0 comments on commit b9ace42

Please sign in to comment.