From fa8abc3b135f5cbd4ee35ea39ec4f0ca4df10196 Mon Sep 17 00:00:00 2001 From: xcx Date: Tue, 24 Mar 2020 20:38:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BB=9F=E4=B8=80=E7=9A=84?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/api.go | 24 +++++++++++++++++++----- demo.go | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/api/api.go b/api/api.go index 6c927a5..ad14a28 100644 --- a/api/api.go +++ b/api/api.go @@ -1,6 +1,7 @@ package api import ( + "encoding/json" "fmt" "math/rand" @@ -38,10 +39,7 @@ type GroupCreateReq struct { ApplyJoinOption string `json:"ApplyJoinOption"` } type GroupCreateAns struct { - ActionStatus string `json:"ActionStatus"` - ErrorInfo string `json:"ErrorInfo"` - ErrorCode int `json:"ErrorCode"` - GroupId string `json:"GroupId"` + GroupId string `json:"GroupId"` } func GroupCreate(r *GroupCreateReq) (*GroupCreateAns, error) { @@ -66,7 +64,23 @@ func Api(servicename, command string, in, out interface{}) error { 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 } diff --git a/demo.go b/demo.go index cfe2bce..9b1cd0a 100644 --- a/demo.go +++ b/demo.go @@ -20,6 +20,6 @@ func main() { Name: "测试聊天室", ApplyJoinOption: "FreeAccess", }) - fmt.Printf("创建房间结果:%x,%+v\n", err, ans) + fmt.Printf("创建房间结果:%v,%+v\n", err, ans) }