mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package interceptor
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
|
|
"go.temporal.io/api/serviceerror"
|
|
serviceerrors "github.com/hanzoai/tasks/common/serviceerror"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
)
|
|
|
|
type (
|
|
ClientStreamErrorInterceptor struct {
|
|
grpc.ClientStream
|
|
}
|
|
)
|
|
|
|
var _ grpc.ClientStream = (*ClientStreamErrorInterceptor)(nil)
|
|
|
|
func NewClientStreamErrorInterceptor(
|
|
clientStream grpc.ClientStream,
|
|
) *ClientStreamErrorInterceptor {
|
|
return &ClientStreamErrorInterceptor{
|
|
ClientStream: clientStream,
|
|
}
|
|
}
|
|
|
|
func (c *ClientStreamErrorInterceptor) CloseSend() error {
|
|
return errorConvert(c.ClientStream.CloseSend())
|
|
}
|
|
|
|
func (c *ClientStreamErrorInterceptor) SendMsg(m any) error {
|
|
return errorConvert(c.ClientStream.SendMsg(m))
|
|
}
|
|
|
|
func (c *ClientStreamErrorInterceptor) RecvMsg(m any) error {
|
|
return errorConvert(c.ClientStream.RecvMsg(m))
|
|
}
|
|
|
|
func StreamErrorInterceptor(
|
|
ctx context.Context,
|
|
desc *grpc.StreamDesc,
|
|
cc *grpc.ClientConn,
|
|
method string,
|
|
streamer grpc.Streamer,
|
|
opts ...grpc.CallOption,
|
|
) (grpc.ClientStream, error) {
|
|
clientStream, err := streamer(ctx, desc, cc, method, opts...)
|
|
if err != nil {
|
|
return nil, errorConvert(err)
|
|
}
|
|
return NewClientStreamErrorInterceptor(clientStream), nil
|
|
}
|
|
|
|
func CustomErrorStreamInterceptor(
|
|
srv any,
|
|
serverStream grpc.ServerStream,
|
|
info *grpc.StreamServerInfo,
|
|
handler grpc.StreamHandler,
|
|
) error {
|
|
err := handler(srv, serverStream)
|
|
if err != nil {
|
|
err = serviceerror.ToStatus(err).Err()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func errorConvert(err error) error {
|
|
switch err {
|
|
case nil:
|
|
return nil
|
|
case io.EOF:
|
|
return io.EOF
|
|
default:
|
|
return FromStatus(status.Convert(err))
|
|
}
|
|
}
|
|
|
|
// FromStatus converts gRPC Status to service error.
|
|
func FromStatus(st *status.Status) error {
|
|
if st == nil {
|
|
return nil
|
|
}
|
|
|
|
switch st.Code() {
|
|
case codes.OK:
|
|
return nil
|
|
case codes.Unknown:
|
|
return serviceerror.NewInternal(st.Message())
|
|
default:
|
|
return serviceerrors.FromStatus(st)
|
|
}
|
|
}
|