增加统一的错误处理

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
import (
"encoding/json"
"fmt"
"math/rand"
@ -38,9 +39,6 @@ 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"`
}
@ -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
}

View File

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