-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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];; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |