Skip to content

Commit

Permalink
add examples/errors (#1077)
Browse files Browse the repository at this point in the history
* addd examples/errors
  • Loading branch information
shenqidebaozi authored Jun 17, 2021
1 parent 8baa2ed commit 7b41acf
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 0 deletions.
136 changes: 136 additions & 0 deletions examples/errors/api/error_reason.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/errors/api/error_reason.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
syntax = "proto3";

package errors;
import "errors.proto";

// 多语言特定包名,用于源代码引用
option go_package = "github.com/go-kratos/kratos/examples/blog/api/v1;v1";
option java_multiple_files = true;
option java_package = "blog.v1.errors";
option objc_class_prefix = "APIBlogErrors";

enum ErrorReason {
option (errors.default_code) = 500;

USER_NOT_FOUND = 0 [(errors.code) = 404];
CONTENT_MISSING = 1 [(errors.code) = 400];;
}
30 changes: 30 additions & 0 deletions examples/errors/api/error_reason_errors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions examples/errors/api/errors.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package errors;

option go_package = "github.com/go-kratos/kratos/v2/errors;v1";
option java_multiple_files = true;
option java_package = "com.github.kratos.errors";
option objc_class_prefix = "KratosErrors";

import "google/protobuf/descriptor.proto";

extend google.protobuf.EnumOptions {
int32 default_code = 1108;
}

extend google.protobuf.EnumValueOptions {
int32 code = 1109;
}
62 changes: 62 additions & 0 deletions examples/errors/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"context"
"github.com/go-kratos/kratos/examples/errors/api"
"github.com/go-kratos/kratos/v2/errors"
"log"

pb "github.com/go-kratos/kratos/examples/helloworld/helloworld"
transgrpc "github.com/go-kratos/kratos/v2/transport/grpc"
transhttp "github.com/go-kratos/kratos/v2/transport/http"
)

func main() {
callHTTP()
callGRPC()
}

func callHTTP() {
conn, err := transhttp.NewClient(
context.Background(),
transhttp.WithEndpoint("127.0.0.1:8000"),
)
if err != nil {
panic(err)
}
client := pb.NewGreeterHTTPClient(conn)
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "empty"})
if err != nil {
if errors.Code(err) == 500 {
log.Println(err)
}
if api.IsUserNotFound(err) {
log.Println("[http] USER_NOT_FOUND_ERROR", err)
}
} else {
log.Printf("[http] SayHello %s\n", reply.Message)
}
}

func callGRPC() {
conn, err := transgrpc.DialInsecure(
context.Background(),
transgrpc.WithEndpoint("127.0.0.1:9000"),
)
if err != nil {
panic(err)
}
client := pb.NewGreeterClient(conn)
reply, err := client.SayHello(context.Background(), &pb.HelloRequest{Name: "kratos"})
if err != nil {
e := errors.FromError(err)
if e.Reason == "USER_NAME_EMPTY" && e.Code == 500 {
log.Println("[grpc] USER_NAME_EMPTY", err)
}
if api.IsUserNotFound(err) {
log.Println("[grpc] USER_NOT_FOUND_ERROR", err)
}
} else {
log.Printf("[grpc] SayHello %+v\n", reply)
}
}
65 changes: 65 additions & 0 deletions examples/errors/server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"context"
"fmt"
"github.com/go-kratos/kratos/v2/errors"
"log"

"github.com/go-kratos/kratos/examples/errors/api"
"github.com/go-kratos/kratos/examples/helloworld/helloworld"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
)

// go build -ldflags "-X main.Version=x.y.z"
var (
// Name is the name of the compiled software.
Name = "errors"
// Version is the version of the compiled software.
Version = "v1.0.0"
)

// server is used to implement helloworld.GreeterServer.
type server struct {
helloworld.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
fmt.Println(in.Name)
if in.Name == "empty" {
// Respond to errors through errors.New().
return nil, errors.New(500, "USER_NAME_EMPTY", "user name is empty")
}
if in.Name == "kratos" {
// Respond to errors with proto generated code.
return nil, api.ErrorUserNotFound("user %s not found", "kratos")
}
return &helloworld.HelloReply{Message: fmt.Sprintf("Hello %+v", in.Name)}, nil
}

func main() {
s := &server{}
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
)
httpSrv := http.NewServer(
http.Address(":8000"),
)
helloworld.RegisterGreeterServer(grpcSrv, s)
helloworld.RegisterGreeterHTTPServer(httpSrv, s)

app := kratos.New(
kratos.Name(Name),
kratos.Server(
httpSrv,
grpcSrv,
),
)

if err := app.Run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 7b41acf

Please sign in to comment.