Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8918f7f891 | |||
| 29a23a75ba | |||
| d9c5d0857b | |||
| 852924e5df |
@@ -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
|
||||
|
||||
+35
-8
@@ -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,20 @@ func NewSDKError(obj map[string]interface{}) *SDKError {
|
||||
return err
|
||||
}
|
||||
|
||||
// Set ErrMsg by msg
|
||||
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)
|
||||
@@ -190,6 +206,14 @@ func Convert(in interface{}, out interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Convert is use convert map[string]interface object to struct
|
||||
func Recover(in interface{}) error {
|
||||
if in == nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New(fmt.Sprint(in))
|
||||
}
|
||||
|
||||
// ReadBody is used read response body
|
||||
func (response *Response) ReadBody() (body []byte, err error) {
|
||||
defer response.Body.Close()
|
||||
@@ -283,7 +307,7 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ConnectTimeout)) * time.Second
|
||||
client.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
client.httpClient.Transport = trans
|
||||
client.ifInit = true
|
||||
}
|
||||
@@ -367,7 +391,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
}
|
||||
dialer, err := proxy.SOCKS5(strings.ToLower(StringValue(runtime.Socks5NetWork)), socks5Proxy.String(), auth,
|
||||
&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond,
|
||||
DualStack: true,
|
||||
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr), IntValue(req.Port)),
|
||||
})
|
||||
@@ -495,11 +519,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 +669,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 +686,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 +895,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
|
||||
}
|
||||
|
||||
+34
-20
@@ -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 {
|
||||
@@ -158,6 +158,9 @@ func TestSDKError(t *testing.T) {
|
||||
})
|
||||
utils.AssertNotNil(t, err)
|
||||
utils.AssertEqual(t, "SDKError:\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())
|
||||
}
|
||||
|
||||
func TestSDKErrorCode404(t *testing.T) {
|
||||
@@ -221,12 +224,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 +369,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 +386,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) {
|
||||
@@ -553,6 +556,17 @@ func Test_Validate(t *testing.T) {
|
||||
utils.AssertNil(t, err)
|
||||
}
|
||||
|
||||
func Test_Recover(t *testing.T) {
|
||||
err := Recover(nil)
|
||||
utils.AssertNil(t, err)
|
||||
defer func() {
|
||||
if r := Recover(recover()); r != nil {
|
||||
utils.AssertEqual(t, "test", r.Error())
|
||||
}
|
||||
}()
|
||||
panic("test")
|
||||
}
|
||||
|
||||
func Test_validate(t *testing.T) {
|
||||
var test *validateTest
|
||||
err := validate(reflect.ValueOf(test))
|
||||
|
||||
Reference in New Issue
Block a user