增加统一的错误处理

This commit is contained in:
xcx 2020-03-24 20:38:26 +08:00
parent 1ee4d0cadd
commit fa8abc3b13
2 changed files with 20 additions and 6 deletions

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"encoding/json"
"fmt" "fmt"
"math/rand" "math/rand"
@ -38,10 +39,7 @@ type GroupCreateReq struct {
ApplyJoinOption string `json:"ApplyJoinOption"` ApplyJoinOption string `json:"ApplyJoinOption"`
} }
type GroupCreateAns struct { type GroupCreateAns struct {
ActionStatus string `json:"ActionStatus"` GroupId string `json:"GroupId"`
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
GroupId string `json:"GroupId"`
} }
func GroupCreate(r *GroupCreateReq) (*GroupCreateAns, error) { func GroupCreate(r *GroupCreateReq) (*GroupCreateAns, error) {
@ -66,7 +64,23 @@ func Api(servicename, command string, in, out interface{}) error {
return err return err
} }
if err := resp.ToJSON(out); err != nil { respBytes := resp.Bytes()
errAns := struct {
ActionStatus string `json:"ActionStatus"`
ErrorInfo string `json:"ErrorInfo"`
ErrorCode int `json:"ErrorCode"`
}{}
if err := json.Unmarshal(respBytes, &errAns); err != nil {
return err
}
if errAns.ErrorCode != 0 {
return fmt.Errorf("ActionStatus:%s,ErrorInfo:%s,ErrorCode:%d", errAns.ActionStatus, errAns.ErrorInfo, errAns.ErrorCode)
}
if err := json.Unmarshal(respBytes, out); err != nil {
return err return err
} }

View File

@ -20,6 +20,6 @@ func main() {
Name: "测试聊天室", Name: "测试聊天室",
ApplyJoinOption: "FreeAccess", ApplyJoinOption: "FreeAccess",
}) })
fmt.Printf("创建房间结果:%x,%+v\n", err, ans) fmt.Printf("创建房间结果:%v,%+v\n", err, ans)
} }