优化error输出
This commit is contained in:
@@ -18,7 +18,7 @@ const (
|
||||
targetDeletePath = "/saas/target/delete"
|
||||
bindSetPath = "/saas/bind/set"
|
||||
bindDeletePath = "/saas/bind/delete"
|
||||
scriptRunPath = "/saas/script/run"
|
||||
scriptDebugPath = "/saas/script/debug"
|
||||
scriptCreatePath = "/saas/script/create"
|
||||
scriptListPath = "/saas/script/list"
|
||||
scriptDeletePath = "/saas/script/delete"
|
||||
@@ -57,7 +57,7 @@ type ApiUrls struct {
|
||||
TargetDeletePath string
|
||||
BindSetPath string
|
||||
BindDeletePath string
|
||||
ScriptRunPath string
|
||||
ScriptDebugPath string
|
||||
ScriptCreatePath string
|
||||
ScriptListPath string
|
||||
ScriptDeletePath string
|
||||
@@ -162,10 +162,10 @@ func InitAPIUrl(c *ApiUrls) *ApiUrls {
|
||||
r.BindDeletePath = bindDeletePath
|
||||
}
|
||||
|
||||
if c.ScriptRunPath != "" {
|
||||
r.ScriptRunPath = c.ScriptRunPath
|
||||
if c.ScriptDebugPath != "" {
|
||||
r.ScriptDebugPath = c.ScriptDebugPath
|
||||
} else {
|
||||
r.ScriptRunPath = scriptRunPath
|
||||
r.ScriptDebugPath = scriptDebugPath
|
||||
}
|
||||
|
||||
if c.ScriptCreatePath != "" {
|
||||
|
||||
@@ -113,8 +113,8 @@ func (c *SaasClient) BindDelete(saasReq *saasapi.SaasReq) (saasRes *saasapi.Saas
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
func (c *SaasClient) ScriptRun(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptRunPath)
|
||||
func (c *SaasClient) ScriptDebug(saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postUrl := c.makeUrl(c.ApiUrls.BaseUrl, c.ApiUrls.ScriptDebugPath)
|
||||
return c.post(postUrl, saasReq)
|
||||
}
|
||||
|
||||
@@ -186,14 +186,12 @@ func (c *SaasClient) makeUrl(baseUrl, path string, params ...string) string {
|
||||
func (c *SaasClient) post(url string, saasReq *saasapi.SaasReq) (saasRes *saasapi.SaasRes, err error) {
|
||||
postBuf, err := proto.Marshal(saasReq)
|
||||
if err != nil {
|
||||
fmt.Println("marshal saas req error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Marshal saas req error. %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(postBuf))
|
||||
if err != nil {
|
||||
fmt.Println("http new request error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http new request error. %w", err)
|
||||
}
|
||||
|
||||
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
@@ -206,30 +204,30 @@ func (c *SaasClient) post(url string, saasReq *saasapi.SaasReq) (saasRes *saasap
|
||||
req.Header.Add("Content-Type", "application/x-protobuf")
|
||||
res, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("http send error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http do request error. %w", err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("NOT 200. %v", res.StatusCode)
|
||||
}
|
||||
|
||||
resBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("http read body error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http read body error. %w", err)
|
||||
}
|
||||
|
||||
saasRes = &saasapi.SaasRes{}
|
||||
if c.ResponseEncoder == RESPONSE_ENCODER_PROTOBUF {
|
||||
err = proto.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to protobuf error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshal response body to protobuf error. %w", err)
|
||||
}
|
||||
} else {
|
||||
err = json.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to json error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshal response body to json error. %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,39 +236,36 @@ func (c *SaasClient) post(url string, saasReq *saasapi.SaasReq) (saasRes *saasap
|
||||
|
||||
func (c *SaasClient) upload(url string, file *os.File, offset int64, size int) (saasRes *saasapi.SaasRes, err error) {
|
||||
if file == nil {
|
||||
return nil, fmt.Errorf("file is nil")
|
||||
return nil, fmt.Errorf("file is nil.")
|
||||
}
|
||||
|
||||
if size <= 0 {
|
||||
return nil, fmt.Errorf("size is invalid")
|
||||
return nil, fmt.Errorf("size is invalid.")
|
||||
}
|
||||
|
||||
buf := make([]byte, size)
|
||||
n, err := file.ReadAt(buf, offset)
|
||||
if err != nil {
|
||||
fmt.Println("read file error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Read file error. %w", err)
|
||||
}
|
||||
if n != size {
|
||||
return nil, fmt.Errorf("read file error")
|
||||
return nil, fmt.Errorf("Read file size not match. %v", n)
|
||||
}
|
||||
|
||||
var gzBuf bytes.Buffer
|
||||
gz := gzip.NewWriter(&gzBuf)
|
||||
|
||||
if _, err := gz.Write(buf); err != nil {
|
||||
fmt.Println("gzip write error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("GZip write error. %w", err)
|
||||
}
|
||||
|
||||
if err = gz.Close(); err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("GZip close error. %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(gzBuf.Bytes()))
|
||||
if err != nil {
|
||||
fmt.Println("http new request error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http new request error. %w", err)
|
||||
}
|
||||
|
||||
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
@@ -284,30 +279,30 @@ func (c *SaasClient) upload(url string, file *os.File, offset int64, size int) (
|
||||
req.Header.Add("Content-Encoding", "gzip")
|
||||
res, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("http send error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http do request error. %w", err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
return nil, fmt.Errorf("NOT 200. %v", res.StatusCode)
|
||||
}
|
||||
|
||||
resBody, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("http read body error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("http read body error. %w", err)
|
||||
}
|
||||
|
||||
saasRes = &saasapi.SaasRes{}
|
||||
if c.ResponseEncoder == RESPONSE_ENCODER_PROTOBUF {
|
||||
err = proto.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to protobuf error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshal response body to protobuf error. %w", err)
|
||||
}
|
||||
} else {
|
||||
err = json.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to json error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshal response body to json error. %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -316,17 +311,16 @@ func (c *SaasClient) upload(url string, file *os.File, offset int64, size int) (
|
||||
|
||||
func (c *SaasClient) download(url string, file *os.File, offset int64, size int) (saasRes *saasapi.SaasRes, err error) {
|
||||
if file == nil {
|
||||
return nil, fmt.Errorf("file is nil")
|
||||
return nil, fmt.Errorf("file is nil.")
|
||||
}
|
||||
|
||||
if size <= 0 {
|
||||
return nil, fmt.Errorf("size is invalid")
|
||||
return nil, fmt.Errorf("size is invalid.")
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("GET", url, bytes.NewBuffer([]byte{}))
|
||||
if err != nil {
|
||||
fmt.Println("http new request error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http new request error. %w", err)
|
||||
}
|
||||
|
||||
timeStamp := strconv.FormatInt(time.Now().Unix(), 10)
|
||||
@@ -340,24 +334,20 @@ func (c *SaasClient) download(url string, file *os.File, offset int64, size int)
|
||||
|
||||
res, err := c.Client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Println("http send error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http do request error. %w", err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != 200 {
|
||||
err = fmt.Errorf("NOT 200. %v", res.StatusCode)
|
||||
fmt.Println("http state error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("http state error. %w", err)
|
||||
}
|
||||
|
||||
bodyReader := res.Body
|
||||
if strings.Contains(res.Header.Get("Content-Encoding"), "gzip") {
|
||||
gz, err := gzip.NewReader(res.Body)
|
||||
if err != nil {
|
||||
fmt.Println("gzip newreader error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("GZip newreader error. %w", err)
|
||||
}
|
||||
defer gz.Close()
|
||||
bodyReader = gz
|
||||
@@ -365,8 +355,7 @@ func (c *SaasClient) download(url string, file *os.File, offset int64, size int)
|
||||
|
||||
resBody, err := io.ReadAll(bodyReader)
|
||||
if err != nil {
|
||||
fmt.Println("read body error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Http read body error. %w", err)
|
||||
}
|
||||
saasRes = &saasapi.SaasRes{}
|
||||
|
||||
@@ -374,22 +363,18 @@ func (c *SaasClient) download(url string, file *os.File, offset int64, size int)
|
||||
if len(resBody) == size {
|
||||
file.WriteAt(resBody, offset)
|
||||
} else {
|
||||
err = fmt.Errorf("body size error. body:%v, want:%v", len(resBody), size)
|
||||
fmt.Println("http read body error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("body size error. body:%v, want:%v", len(resBody), size)
|
||||
}
|
||||
} else {
|
||||
if c.ResponseEncoder == RESPONSE_ENCODER_PROTOBUF {
|
||||
err = proto.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to protobuf error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshal response body to protobuf error. %w", err)
|
||||
}
|
||||
} else {
|
||||
err = json.Unmarshal(resBody, saasRes)
|
||||
if err != nil {
|
||||
fmt.Println("unmarshal response body to json error", err)
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("unmarshal response body to json error. %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user