Files
tasks/common/rpc/interceptor/frontend_service_error.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
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).
2026-03-19 10:36:40 -07:00

61 lines
2.0 KiB
Go

package interceptor
import (
"context"
"go.temporal.io/api/serviceerror"
"github.com/hanzoai/tasks/common/api"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/log/tag"
serviceerrors "github.com/hanzoai/tasks/common/serviceerror"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
const (
// ResourceExhaustedCauseHeader is added to rpc response if request returns ResourceExhausted error.
ResourceExhaustedCauseHeader = "X-Resource-Exhausted-Cause"
// ResourceExhaustedScopeHeader is added to rpc response if request returns ResourceExhausted error.
ResourceExhaustedScopeHeader = "X-Resource-Exhausted-Scope"
)
// NewFrontendServiceErrorInterceptor returns a gRPC interceptor that has two responsibilities:
// 1. Mask certain internal service error details.
// 2. Propagate resource exhaustion details via gRPC headers.
func NewFrontendServiceErrorInterceptor(
logger log.Logger,
) grpc.UnaryServerInterceptor {
return func(
ctx context.Context,
req any,
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (any, error) {
resp, err := handler(ctx, req)
if err == nil {
return resp, nil
}
switch serviceErr := err.(type) {
case *serviceerrors.ShardOwnershipLost:
err = serviceerror.NewUnavailable("shard unavailable, please backoff and retry")
case *serviceerror.DataLoss:
err = serviceerror.NewUnavailable("internal history service error")
case *serviceerror.ResourceExhausted:
if headerErr := grpc.SetHeader(ctx, metadata.Pairs(
ResourceExhaustedCauseHeader, serviceErr.Cause.String(),
ResourceExhaustedScopeHeader, serviceErr.Scope.String(),
)); headerErr != nil {
// So while this is *not* a user-facing error or problem in itself,
// it indicates that there might be larger connection issues at play.
logger.Error("Failed to add Resource-Exhausted headers to response",
tag.Operation(api.MethodName(info.FullMethod)),
tag.Error(headerErr))
}
}
return resp, err
}
}