Skip to content

Commit

Permalink
fix: wrap the errors (#387)
Browse files Browse the repository at this point in the history
* fix: fix the err format

* fix: change the error format

* fix: wrap the errors

* fix: wrap the flagParse error

* fix: fix the InitFromConfig error format
  • Loading branch information
luhaoling committed Jan 22, 2024
1 parent a46e5bb commit e081661
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 31 deletions.
5 changes: 3 additions & 2 deletions cmd/api/admin-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"fmt"
"github.com/OpenIMSDK/tools/errs"
"math/rand"
"net"
"strconv"
Expand Down Expand Up @@ -66,7 +67,7 @@ func main() {
panic(err)
}
if err := log.InitFromConfig("chat.log", "admin-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(err)
panic(fmt.Errorf("InitFromConfig failed:%w", err))
}
if config.Config.Envs.Discovery == "k8s" {
ginPort = 80
Expand All @@ -80,7 +81,7 @@ func main() {
}

if err := zk.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
panic(err)
panic(errs.Wrap(err, "CreateRpcRootNodes error"))
}
zk.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) // 默认RPC中间件
engine := gin.Default()
Expand Down
5 changes: 3 additions & 2 deletions cmd/api/chat-api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
import (
"flag"
"fmt"
"github.com/OpenIMSDK/tools/errs"
"math/rand"
"net"
"strconv"
Expand Down Expand Up @@ -77,7 +78,7 @@ func main() {
panic(err)
}
if err := log.InitFromConfig("chat.log", "chat-api", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(err)
panic(fmt.Errorf("InitFromConfig failed:%w", err))
}
if config.Config.Envs.Discovery == "k8s" {
ginPort = 80
Expand All @@ -91,7 +92,7 @@ func main() {
panic(err)
}
if err := zk.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
panic(err)
panic(errs.Wrap(err, "CreateRpcRootNodes error"))
}
zk.AddOption(mw.GrpcClient(), grpc.WithTransportCredentials(insecure.NewCredentials())) // 默认RPC中间件
engine := gin.Default()
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpc/admin-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
rpcPort = 80
}
if err := log.InitFromConfig("chat.log", "admin-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(err)
panic(fmt.Errorf("InitFromConfig failed:%w", err))
}
err = chatrpcstart.Start(rpcPort, config.Config.RpcRegisterName.OpenImAdminName, 0, admin.Start)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/rpc/chat-rpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
panic(err)
}
if err := log.InitFromConfig("chat.log", "chat-rpc", *config.Config.Log.RemainLogLevel, *config.Config.Log.IsStdout, *config.Config.Log.IsJson, *config.Config.Log.StorageLocation, *config.Config.Log.RemainRotationCount, *config.Config.Log.RotationTime); err != nil {
panic(err)
panic(fmt.Errorf("InitFromConfig failed:%w", err))
}
err = chatrpcstart.Start(rpcPort, config.Config.RpcRegisterName.OpenImChatName, 0, chat.Start)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions internal/rpc/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
func Start(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
db, err := dbconn.NewGormDB()
if err != nil {
return err
return errs.Wrap(err)
}
tables := []any{
admin2.Admin{},
Expand All @@ -56,17 +56,17 @@ func Start(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
admin2.ClientConfig{},
}
if err := db.AutoMigrate(tables...); err != nil {
return err
return errs.Wrap(err)
}
rdb, err := cache.NewRedis()
if err != nil {
return err
return errs.Wrap(err)
}
if err := database.NewAdminDatabase(db, rdb).InitAdmin(context.Background()); err != nil {
return err
}
if err := discov.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
panic(err)
panic(errs.Wrap(err, "CreateRpcRootNodes error"))
}

admin.RegisterAdminServer(server, &adminServer{Database: database.NewAdminDatabase(db, rdb),
Expand Down
14 changes: 6 additions & 8 deletions internal/rpc/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package chat
import (
"github.com/OpenIMSDK/chat/pkg/common/apicall"
"github.com/OpenIMSDK/tools/discoveryregistry"
"github.com/OpenIMSDK/tools/errs"
"google.golang.org/grpc"

"github.com/OpenIMSDK/chat/pkg/common/config"
Expand All @@ -32,7 +33,7 @@ import (
func Start(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) error {
db, err := dbconn.NewGormDB()
if err != nil {
return err
return errs.Wrap(err)
}
tables := []any{
chat2.Account{},
Expand All @@ -43,18 +44,15 @@ func Start(discov discoveryregistry.SvcDiscoveryRegistry, server *grpc.Server) e
chat2.Log{},
}
if err := db.AutoMigrate(tables...); err != nil {
return err
return errs.Wrap(err)
}
s, err := sms.New()
if err != nil {
return err
}
email, err := email.NewMail()
if err != nil {
return err
return errs.Wrap(err)
}
email := email.NewMail()
if err := discov.CreateRpcRootNodes([]string{config.Config.RpcRegisterName.OpenImAdminName, config.Config.RpcRegisterName.OpenImChatName}); err != nil {
panic(err)
panic(errs.Wrap(err, "CreateRpcRootNodes error"))
}
chat.RegisterChatServer(server, &chatSvr{
Database: database.NewChatDatabase(db),
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/chatrpcstart/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Start(rpcPort int, rpcRegisterName string, prometheusPort int, rpcFn func(c
defer srv.GracefulStop()
err = rpcFn(zkClient, srv)
if err != nil {
return errs.Wrap(err)
return err
}
err = zkClient.Register(rpcRegisterName, registerIP, rpcPort, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions pkg/common/config/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ func InitConfig(configFile string) error {
if err := yaml.NewDecoder(bytes.NewReader(data)).Decode(&Config); err != nil {
return fmt.Errorf("parse loacl openIMConfig file error: %w", err)
}
if err != nil {
return utils.Wrap(err, configFile)
}

if err := configGetEnv(); err != nil {
return fmt.Errorf("get env error:%w", err)
Expand Down Expand Up @@ -180,7 +177,6 @@ func findConfigPath(configFile string) (string, error) {
if _, err := findConfigFile([]string{configFile}); err != nil {
return "", errs.Wrap(errors.New("the configFile argument path is error"))
}
fmt.Println("configfile:", configFile)
return configFile, nil
}

Expand All @@ -197,7 +193,7 @@ func findConfigPath(configFile string) (string, error) {

p1, err := os.Executable()
if err != nil {
return "", err
return "", errs.Wrap(err)
}

path = CreateCatalogPath(p1)
Expand Down Expand Up @@ -277,7 +273,6 @@ func getArrEnv(key1, key2 string, fallback []string) {
return
}
arr[0] = str
fmt.Println("zookeeper Envirement valiable", "str", str)
Config.Zookeeper.ZkAddr = arr
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/email/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ import (
"gopkg.in/gomail.v2"
)

func NewMail() (Mail, error) {
func NewMail() Mail {
dail := gomail.NewDialer(
config.Config.VerifyCode.Mail.SmtpAddr,
config.Config.VerifyCode.Mail.SmtpPort,
config.Config.VerifyCode.Mail.SenderMail,
config.Config.VerifyCode.Mail.SenderAuthorizationCode)

return &mail{dail: dail}, nil
return &mail{dail: dail}
}

type Mail interface {
Expand Down
5 changes: 1 addition & 4 deletions pkg/email/mail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ func TestEmail(T *testing.T) {
want: errors.New("dial tcp :0: connectex: The requested address is not valid in its context."),
},
}
mail, err := NewMail()
if err != nil {
T.Errorf("Init mail failed,%v", err)
}
mail := NewMail()

for _, tt := range tests {
T.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit e081661

Please sign in to comment.