diff --git a/_examples/cluster-reentrancy/hello_grain.pb.go b/_examples/cluster-reentrancy/hello_grain.pb.go index e6335e9e..1aed4d75 100644 --- a/_examples/cluster-reentrancy/hello_grain.pb.go +++ b/_examples/cluster-reentrancy/hello_grain.pb.go @@ -1,12 +1,13 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.3.0 +// protoc-gen-grain v0.4.0 // protoc v4.25.0 // source: hello.proto package main import ( + errors "errors" fmt "fmt" actor "github.com/asynkron/protoactor-go/actor" cluster "github.com/asynkron/protoactor-go/cluster" @@ -101,8 +102,8 @@ func (g *HelloGrainClient) InvokeService(r *InvokeServiceRequest, opts ...cluste switch msg := resp.(type) { case *InvokeServiceResponse: return msg, nil - case error: - return nil, fmt.Errorf("error response: %w", msg) + case *cluster.GrainErrorResponse: + return nil, errors.New(msg.Err) default: return nil, fmt.Errorf("unknown response type %T", resp) } @@ -138,8 +139,8 @@ func (g *HelloGrainClient) DoWork(r *DoWorkRequest, opts ...cluster.GrainCallOpt switch msg := resp.(type) { case *DoWorkResponse: return msg, nil - case error: - return nil, fmt.Errorf("error response: %w", msg) + case *cluster.GrainErrorResponse: + return nil, errors.New(msg.Err) default: return nil, fmt.Errorf("unknown response type %T", resp) } @@ -178,12 +179,14 @@ func (a *HelloActor) Receive(ctx actor.Context) { err := proto.Unmarshal(msg.MessageData, req) if err != nil { ctx.Logger().Error("[Grain] InvokeService(InvokeServiceRequest) proto.Unmarshal failed.", slog.Any("error", err)) - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } err = a.inner.InvokeService(req, respond[*InvokeServiceResponse](a.ctx), a.onError, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } case 1: @@ -191,13 +194,15 @@ func (a *HelloActor) Receive(ctx actor.Context) { err := proto.Unmarshal(msg.MessageData, req) if err != nil { ctx.Logger().Error("[Grain] DoWork(DoWorkRequest) proto.Unmarshal failed.", slog.Any("error", err)) - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } r0, err := a.inner.DoWork(req, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } ctx.Respond(r0) @@ -210,7 +215,8 @@ func (a *HelloActor) Receive(ctx actor.Context) { // onError should be used in ctx.ReenterAfter // you can just return error in reenterable method for other errors func (a *HelloActor) onError(err error) { - a.ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + a.ctx.Respond(resp) } func respond[T proto.Message](ctx cluster.GrainContext) func(T) { diff --git a/_examples/cluster-reentrancy/main.go b/_examples/cluster-reentrancy/main.go index d6dc29ae..5fa4cf0e 100644 --- a/_examples/cluster-reentrancy/main.go +++ b/_examples/cluster-reentrancy/main.go @@ -18,11 +18,10 @@ func main() { helloKind := NewHelloKind(NewHelloGrain, 0) clusterConfig := cluster.Configure("core", provider, lookup, config, cluster.WithKinds( helloKind)) - cluster := cluster.New(system, clusterConfig) - cluster.StartMember() - + cst := cluster.New(system, clusterConfig) + cst.StartMember() // self call: request -> 1 -> 1 - client := GetHelloGrainClient(cluster, "1") + client := GetHelloGrainClient(cst, "1") resp, err := client.InvokeService(&InvokeServiceRequest{Name: "Alice"}) if err != nil { panic(err) @@ -30,7 +29,7 @@ func main() { fmt.Printf("%v\n", resp) // reenter call: request -> 2 -> 1 -> 2 - client = GetHelloGrainClient(cluster, "2") + client = GetHelloGrainClient(cst, "2") resp, err = client.InvokeService(&InvokeServiceRequest{Name: "Alice"}) if err != nil { panic(err) diff --git a/protobuf/protoc-gen-go-grain/generate.go b/protobuf/protoc-gen-go-grain/generate.go index 12aea1ca..9753e9b8 100644 --- a/protobuf/protoc-gen-go-grain/generate.go +++ b/protobuf/protoc-gen-go-grain/generate.go @@ -66,6 +66,7 @@ func generateContent(gen *protogen.Plugin, g *protogen.GeneratedFile, file *prot g.QualifiedGoIdent(fmtPackage.Ident("")) g.QualifiedGoIdent(timePackage.Ident("")) g.QualifiedGoIdent(slogPackage.Ident("")) + g.QualifiedGoIdent(errorsPackage.Ident("")) for _, service := range file.Services { generateService(service, file, g) diff --git a/protobuf/protoc-gen-go-grain/templates/grain.tmpl b/protobuf/protoc-gen-go-grain/templates/grain.tmpl index 300973f3..b2fec6f2 100644 --- a/protobuf/protoc-gen-go-grain/templates/grain.tmpl +++ b/protobuf/protoc-gen-go-grain/templates/grain.tmpl @@ -91,8 +91,8 @@ func (g *{{ $service.Name }}GrainClient) {{ $method.Name }}(r *{{ $method.Input switch msg := resp.(type) { case *{{ $method.Output }}: return msg, nil - case error: - return nil, fmt.Errorf("error response: %w", msg) + case *cluster.GrainErrorResponse: + return nil, errors.New(msg.Err) default: return nil, fmt.Errorf("unknown response type %T", resp) } @@ -132,19 +132,22 @@ func (a *{{ $service.Name }}Actor) Receive(ctx actor.Context) { err := proto.Unmarshal(msg.MessageData, req) if err != nil { ctx.Logger().Error("[Grain] {{ $method.Name }}({{ $method.Input }}) proto.Unmarshal failed.", slog.Any("error", err)) - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } {{ if $method.Options.Reenterable -}} err = a.inner.{{ $method.Name }}(req, respond[*{{ $method.Output }}](a.ctx), a.onError, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } {{ else }} r0, err := a.inner.{{ $method.Name }}(req, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } ctx.Respond(r0) @@ -159,7 +162,8 @@ func (a *{{ $service.Name }}Actor) Receive(ctx actor.Context) { // onError should be used in ctx.ReenterAfter // you can just return error in reenterable method for other errors func (a *{{ $service.Name }}Actor) onError(err error) { - a.ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + a.ctx.Respond(resp) } func respond[T proto.Message](ctx cluster.GrainContext) func (T) { diff --git a/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go b/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go index 82da47e2..5d3c67c8 100644 --- a/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/hello/hello_grain.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.3.0 +// protoc-gen-grain v0.4.0 // protoc v4.25.0 // source: testdata/hello/hello.proto @@ -85,8 +85,8 @@ func (g *HelloGrainClient) SayHello(r *emptypb.Empty, opts ...cluster.GrainCallO switch msg := resp.(type) { case *SayHelloResponse: return msg, nil - case error: - return nil, fmt.Errorf("error response: %w", msg) + case *cluster.GrainErrorResponse: + return nil, errors.New(msg.Err) default: return nil, fmt.Errorf("unknown response type %T", resp) } @@ -125,13 +125,15 @@ func (a *HelloActor) Receive(ctx actor.Context) { err := proto.Unmarshal(msg.MessageData, req) if err != nil { ctx.Logger().Error("[Grain] SayHello(emptypb.Empty) proto.Unmarshal failed.", slog.Any("error", err)) - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } r0, err := a.inner.SayHello(req, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } ctx.Respond(r0) @@ -144,7 +146,8 @@ func (a *HelloActor) Receive(ctx actor.Context) { // onError should be used in ctx.ReenterAfter // you can just return error in reenterable method for other errors func (a *HelloActor) onError(err error) { - a.ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + a.ctx.Respond(resp) } func respond[T proto.Message](ctx cluster.GrainContext) func(T) { diff --git a/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go b/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go index 366f42be..06d0c835 100644 --- a/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go +++ b/protobuf/protoc-gen-go-grain/testdata/reenter/hello_grain.pb.go @@ -1,6 +1,6 @@ // Code generated by protoc-gen-grain. DO NOT EDIT. // versions: -// protoc-gen-grain v0.3.0 +// protoc-gen-grain v0.4.0 // protoc v4.25.0 // source: testdata/reenter/hello.proto @@ -85,8 +85,8 @@ func (g *HelloGrainClient) SayHello(r *SayHelloRequest, opts ...cluster.GrainCal switch msg := resp.(type) { case *SayHelloResponse: return msg, nil - case error: - return nil, fmt.Errorf("error response: %w", msg) + case *cluster.GrainErrorResponse: + return nil, errors.New(msg.Err) default: return nil, fmt.Errorf("unknown response type %T", resp) } @@ -122,8 +122,8 @@ func (g *HelloGrainClient) Dowork(r *DoworkRequest, opts ...cluster.GrainCallOpt switch msg := resp.(type) { case *DoworkResponse: return msg, nil - case error: - return nil, fmt.Errorf("error response: %w", msg) + case *cluster.GrainErrorResponse: + return nil, errors.New(msg.Err) default: return nil, fmt.Errorf("unknown response type %T", resp) } @@ -162,12 +162,14 @@ func (a *HelloActor) Receive(ctx actor.Context) { err := proto.Unmarshal(msg.MessageData, req) if err != nil { ctx.Logger().Error("[Grain] SayHello(SayHelloRequest) proto.Unmarshal failed.", slog.Any("error", err)) - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } err = a.inner.SayHello(req, respond[*SayHelloResponse](a.ctx), a.onError, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } case 1: @@ -175,13 +177,15 @@ func (a *HelloActor) Receive(ctx actor.Context) { err := proto.Unmarshal(msg.MessageData, req) if err != nil { ctx.Logger().Error("[Grain] Dowork(DoworkRequest) proto.Unmarshal failed.", slog.Any("error", err)) - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } r0, err := a.inner.Dowork(req, a.ctx) if err != nil { - ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + ctx.Respond(resp) return } ctx.Respond(r0) @@ -194,7 +198,8 @@ func (a *HelloActor) Receive(ctx actor.Context) { // onError should be used in ctx.ReenterAfter // you can just return error in reenterable method for other errors func (a *HelloActor) onError(err error) { - a.ctx.Respond(err) + resp := &cluster.GrainErrorResponse{Err: err.Error()} + a.ctx.Respond(resp) } func respond[T proto.Message](ctx cluster.GrainContext) func(T) { diff --git a/protobuf/protoc-gen-go-grain/version.go b/protobuf/protoc-gen-go-grain/version.go index a90b5b3c..e3e9e935 100644 --- a/protobuf/protoc-gen-go-grain/version.go +++ b/protobuf/protoc-gen-go-grain/version.go @@ -1,3 +1,3 @@ package main -const version = "v0.3.0" +const version = "v0.4.0"