Compare commits

...

2 Commits

Author SHA1 Message Date
wb-wzc505509 d9c5d0857b add SetErrMsg to SDKError 2020-07-03 13:21:52 +08:00
wb-wzc505509 852924e5df fix the error caused by omitempty 2020-06-22 13:03:43 +08:00
3 changed files with 46 additions and 26 deletions
+2
View File
@@ -1,5 +1,7 @@
module github.com/alibabacloud-go/tea
go 1.14
require (
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
+24 -6
View File
@@ -71,6 +71,8 @@ type SDKError struct {
Code *string
Message *string
Data *string
Stack *string
errMsg *string
}
// RuntimeObject is used for converting http configuration
@@ -178,6 +180,19 @@ func NewSDKError(obj map[string]interface{}) *SDKError {
return err
}
func (err *SDKError) SetErrMsg(msg string) {
err.errMsg = String(msg)
}
func (err *SDKError) Error() string {
if err.errMsg == nil {
str := fmt.Sprintf("SDKError:\n Code: %s\n Message: %s\n Data: %s\n",
StringValue(err.Code), StringValue(err.Message), StringValue(err.Data))
err.SetErrMsg(str)
}
return StringValue(err.errMsg)
}
// Return message of CastError
func (err *CastError) Error() string {
return StringValue(err.Message)
@@ -495,11 +510,6 @@ func setDialContext(runtime *RuntimeObject, port int) func(cxt context.Context,
}
}
func (err *SDKError) Error() string {
return fmt.Sprintf("SDKError:\n Code: %s\n Message: %s\n Data: %s\n",
StringValue(err.Code), StringValue(err.Message), StringValue(err.Data))
}
func ToObject(obj interface{}) map[string]interface{} {
result := make(map[string]interface{})
byt, _ := json.Marshal(obj)
@@ -650,6 +660,9 @@ func structToMap(dataValue reflect.Value) map[string]interface{} {
return out
}
if dataValue.Kind().String() == "ptr" {
if dataValue.IsNil() {
return out
}
dataValue = dataValue.Elem()
}
if !dataValue.IsValid() {
@@ -664,9 +677,12 @@ func structToMap(dataValue reflect.Value) map[string]interface{} {
name, containsNameTag := field.Tag.Lookup("json")
if !containsNameTag {
name = field.Name
} else {
strs := strings.Split(name, ",")
name = strs[0]
}
fieldValue := dataValue.FieldByName(field.Name)
if !fieldValue.IsValid() {
if !fieldValue.IsValid() || fieldValue.IsNil() {
continue
}
if field.Type.Kind().String() == "struct" {
@@ -870,6 +886,8 @@ func validatePtr(elementValue reflect.Value, containsregexpTag bool, tag, tagNam
func checkRequire(field reflect.StructField, valueField reflect.Value) error {
name, _ := field.Tag.Lookup("json")
strs := strings.Split(name, ",")
name = strs[0]
if !valueField.IsNil() && valueField.IsValid() {
return nil
}
+20 -20
View File
@@ -18,8 +18,8 @@ import (
)
type test struct {
Key string `json:"key"`
Body []byte `json:"body"`
Key string `json:"key,omitempty"`
Body []byte `json:"body,omitempty"`
}
type PrettifyTest struct {
@@ -46,16 +46,16 @@ var runtimeObj = map[string]interface{}{
}
type validateTest struct {
Num1 *int `json:"num1" require:"true" minimum:"2"`
Num2 *int `json:"num2" maximum:"6"`
Name1 *string `json:"name1" maxLength:"4"`
Name2 *string `json:"name2" minLength:"2"`
Str *string `json:"str" pattern:"^[a-d]*$" maxLength:"4"`
MaxLength *errMaxLength `json:"MaxLength"`
MinLength *errMinLength `json:"MinLength"`
Maximum *errMaximum `json:"Maximum"`
Minimum *errMinimum `json:"Minimum"`
List []*string `json:"list" pattern:"^[a-d]*$" maxLength:"4"`
Num1 *int `json:"num1,omitempty" require:"true" minimum:"2"`
Num2 *int `json:"num2,omitempty" maximum:"6"`
Name1 *string `json:"name1,omitempty" maxLength:"4"`
Name2 *string `json:"name2,omitempty" minLength:"2"`
Str *string `json:"str,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
MinLength *errMinLength `json:"MinLength,omitempty"`
Maximum *errMaximum `json:"Maximum,omitempty"`
Minimum *errMinimum `json:"Minimum,omitempty"`
List []*string `json:"list,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
}
type errMaxLength struct {
@@ -221,12 +221,12 @@ func TestMerge(t *testing.T) {
}
type Test struct {
Msg *string `json:"Msg"`
Cast *CastError `json:"Cast"`
ListPtr []*string `json:"ListPtr"`
List []string `json:"List"`
CastList []CastError `json:"CastList"`
CastListPtr []*CastError `json:"CastListPtr"`
Msg *string `json:"Msg,omitempty"`
Cast *CastError `json:"Cast,omitempty"`
ListPtr []*string `json:"ListPtr,omitempty"`
List []string `json:"List,omitempty"`
CastList []CastError `json:"CastList,omitempty"`
CastListPtr []*CastError `json:"CastListPtr,omitempty"`
}
func TestToMap(t *testing.T) {
@@ -366,7 +366,7 @@ func Test_DoRequest(t *testing.T) {
runtimeObj["httpsProxy"] = "# #%gfdf"
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, resp)
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
utils.AssertContains(t, err.Error(), `invalid URL escape "%gf"`)
request.Pathname = String("?log")
request.Headers["tea"] = String("")
@@ -383,7 +383,7 @@ func Test_DoRequest(t *testing.T) {
runtimeObj["socks5Proxy"] = "# #%gfdf"
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, resp)
utils.AssertEqual(t, `parse # #%gfdf: invalid URL escape "%gf"`, err.Error())
utils.AssertContains(t, err.Error(), ` invalid URL escape "%gf"`)
hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
return func(req *http.Request) (*http.Response, error) {