Skip to content

Commit ec2b7b0

Browse files
author
hero
committed
修改错误提示
1 parent 080aa27 commit ec2b7b0

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

micro_v2/client/main.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import (
66
"github.com/micro/go-micro/v2/client"
77
"github.com/micro/go-micro/v2/client/grpc"
88
"github.com/micro/go-micro/v2/client/selector"
9+
"github.com/micro/go-micro/v2/errors"
910
"github.com/micro/go-micro/v2/registry"
1011
"github.com/micro/go-micro/v2/registry/etcd"
12+
"log"
1113
test_agent "micro_v2"
12-
"os"
1314
"time"
1415
)
1516

@@ -22,20 +23,24 @@ func main() {
2223
selector.Registry(micReg),
2324
)),
2425
))
25-
2626
var opss client.CallOption = func(o *client.CallOptions) {
2727
o.RequestTimeout = time.Second * 30
2828
o.DialTimeout = time.Second * 30
2929
o.Retries = 3
30-
o.Address = []string{"127.0.0.1:8081"}
30+
o.Address = []string{"127.0.0.1:8080"}
3131
}
3232
info, err := agent.RpcUserInfo(context.TODO(), &test_agent.ReqMsg{
3333
UserName: "test user",
3434
}, opss)
3535
fmt.Println(time.Now().Format("2006-01-02 15:04:05"))
3636
if err != nil {
37-
fmt.Println(err)
38-
os.Exit(0)
37+
s := errors.FromError(err)
38+
if s != nil { // 可转为Status
39+
log.Println(s.GetCode())
40+
log.Println(s.String())
41+
} else { // 普通error
42+
log.Println(err)
43+
}
3944
}
4045
fmt.Println(info)
4146
}

micro_v2/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ go 1.14
55
require (
66
github.com/golang/protobuf v1.4.2
77
github.com/micro/go-micro/v2 v2.8.0
8+
google.golang.org/grpc v1.26.0
89
)

micro_v2/server/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"github.com/micro/go-micro/v2/errors"
67
"github.com/micro/go-micro/v2/registry"
78
"github.com/micro/go-micro/v2/registry/etcd"
89
"github.com/micro/go-micro/v2/service"
@@ -21,7 +22,8 @@ func (a *Agent) RpcUserInfo(ctx context.Context, in *test_agent.ReqMsg, out *tes
2122
}
2223
out.Info = "test success"
2324
fmt.Println("server RpcUserInfo", "in", in, "out", out)
24-
return nil
25+
26+
return errors.New("info", "123", 600)
2527
}
2628

2729
func main() {

0 commit comments

Comments
 (0)