浏览代码

修改报错

yjp 3 年之前
父节点
当前提交
5c8b772ec7
共有 4 个文件被更改,包括 21 次插入10 次删除
  1. 2 2
      invoke/invoke.go
  2. 2 2
      pubsub/pubsub.go
  3. 6 6
      state/state.go
  4. 11 0
      utils/utils.go

+ 2 - 2
invoke/invoke.go

@@ -1,7 +1,7 @@
 package invoke
 
 import (
-	"errors"
+	"git.sxidc.com/service-supports/dapr_api/utils"
 	"github.com/go-resty/resty/v2"
 	"net/url"
 	"time"
@@ -59,7 +59,7 @@ func (api *API) Post(methodName string, headers map[string]string, data []byte)
 	}
 
 	if resp.IsError() {
-		return nil, errors.New(string(resp.Body()))
+		return nil, utils.ResponseStatusError(resp)
 	}
 
 	return resp.Body(), nil

+ 2 - 2
pubsub/pubsub.go

@@ -1,8 +1,8 @@
 package pubsub
 
 import (
-	"errors"
 	"fmt"
+	"git.sxidc.com/service-supports/dapr_api/utils"
 	"github.com/go-resty/resty/v2"
 	"time"
 )
@@ -54,7 +54,7 @@ func (api *API) publish(pubsubName string, topic string, contentType string, dat
 	}
 
 	if resp.IsError() {
-		return errors.New(string(resp.Body()))
+		return utils.ResponseStatusError(resp)
 	}
 
 	return nil

+ 6 - 6
state/state.go

@@ -2,8 +2,8 @@ package state
 
 import (
 	"encoding/json"
-	"errors"
 	"fmt"
+	"git.sxidc.com/service-supports/dapr_api/utils"
 	resty "github.com/go-resty/resty/v2"
 	"strconv"
 	"strings"
@@ -51,7 +51,7 @@ func (api *API) SaveState(storeName string, request []SaveStateRequest) error {
 	}
 
 	if resp.IsError() {
-		return errors.New(string(resp.Body()))
+		return utils.ResponseStatusError(resp)
 	}
 
 	return nil
@@ -68,7 +68,7 @@ func (api *API) GetState(storeName string, key string, queryParams map[string]st
 	}
 
 	if resp.IsError() {
-		return "", "", errors.New(string(resp.Body()))
+		return "", "", utils.ResponseStatusError(resp)
 	}
 
 	var body string
@@ -97,7 +97,7 @@ func (api *API) GetStateBulk(storeName string, queryParams map[string]string, re
 	}
 
 	if resp.IsError() {
-		return nil, errors.New(string(resp.Body()))
+		return nil, utils.ResponseStatusError(resp)
 	}
 
 	items := make([]GetStateBulkItem, 0)
@@ -122,7 +122,7 @@ func (api *API) DeleteState(storeName string, key string, queryParams map[string
 	}
 
 	if resp.IsError() {
-		return errors.New(string(resp.Body()))
+		return utils.ResponseStatusError(resp)
 	}
 
 	return nil
@@ -140,7 +140,7 @@ func (api *API) Transaction(storeName string, request TransactionRequest) error
 	}
 
 	if resp.IsError() {
-		return errors.New(string(resp.Body()))
+		return utils.ResponseStatusError(resp)
 	}
 
 	return nil

+ 11 - 0
utils/utils.go

@@ -1,6 +1,9 @@
 package utils
 
 import (
+	"errors"
+	"fmt"
+	"github.com/go-resty/resty/v2"
 	uuid "github.com/satori/go.uuid"
 	"strings"
 )
@@ -42,3 +45,11 @@ func GetUUID() string {
 func SimpleUUID() string {
 	return strings.ReplaceAll(GetUUID(), "-", "")
 }
+
+func ResponseStatusError(resp *resty.Response) error {
+	if resp.Body() != nil && len(resp.Body()) != 0 {
+		return errors.New(string(resp.Body()))
+	} else {
+		return fmt.Errorf("Status: %d %s\n", resp.StatusCode(), resp.Status())
+	}
+}