Compare commits

..

5 Commits

Author SHA1 Message Date
wb-wzc505509 8291a17aca validate support null param 2020-07-09 13:52:35 +08:00
wb-wzc505509 7ad16974b6 validate support null param 2020-07-09 13:39:59 +08:00
wb-wzc505509 8918f7f891 fix timeout error 2020-07-08 17:20:59 +08:00
wb-wzc505509 29a23a75ba add recover 2020-07-06 10:25:53 +08:00
wb-wzc505509 d9c5d0857b add SetErrMsg to SDKError 2020-07-03 13:21:52 +08:00
2 changed files with 54 additions and 9 deletions
+34 -9
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,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)
@@ -759,8 +778,14 @@ func Sleep(backoffTime *int) {
}
func Validate(params interface{}) error {
requestValue := reflect.ValueOf(params).Elem()
err := validate(requestValue)
if params == nil {
return nil
}
requestValue := reflect.ValueOf(params)
if requestValue.IsNil() {
return nil
}
err := validate(requestValue.Elem())
return err
}
+20
View File
@@ -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) {
@@ -551,6 +554,23 @@ func Test_Validate(t *testing.T) {
}
err := Validate(config)
utils.AssertNil(t, err)
err = Validate(new(validateTest))
utils.AssertEqual(t, err.Error(), "num1 should be setted")
err = Validate(nil)
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) {