package errorinjectors

// Code generated by gowrap. DO NOT EDIT.
// template: ../../templates/errorinjectors.tmpl
// gowrap: http://github.com/hexdigest/gowrap

import (
	"context"

	"go.uber.org/yarpc"

	"github.com/uber/cadence/client/sharddistributorexecutor"
	"github.com/uber/cadence/common/errors"
	"github.com/uber/cadence/common/log"
	"github.com/uber/cadence/common/log/tag"
	"github.com/uber/cadence/common/types"
)

const (
	msgShardDistributorExecutorInjectedFakeErr = "Injected fake sharddistributorexecutor client error"
)

// sharddistributorexecutorClient implements sharddistributorexecutor.Client interface instrumented with retries
type sharddistributorexecutorClient struct {
	client        sharddistributorexecutor.Client
	errorRate     float64
	logger        log.Logger
	fakeErrFn     func(float64) error
	forwardCallFn func(error) bool
}

// NewShardDistributorExecutorClient creates a new instance of sharddistributorexecutorClient that injects error into every call with a given rate.
func NewShardDistributorExecutorClient(client sharddistributorexecutor.Client, errorRate float64, logger log.Logger) sharddistributorexecutor.Client {
	return &sharddistributorexecutorClient{
		client:        client,
		errorRate:     errorRate,
		logger:        logger,
		fakeErrFn:     errors.GenerateFakeError,
		forwardCallFn: errors.ShouldForwardCall,
	}
}

func (c *sharddistributorexecutorClient) Heartbeat(ctx context.Context, ep1 *types.ExecutorHeartbeatRequest, p1 ...yarpc.CallOption) (ep2 *types.ExecutorHeartbeatResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		ep2, err = c.client.Heartbeat(ctx, ep1, p1...)
	}

	if fakeErr != nil {
		c.logger.Error(msgShardDistributorExecutorInjectedFakeErr,
			tag.ShardDistributorExecutorClientOperationHeartbeat,
			tag.Error(fakeErr),
			tag.Bool(forwardCall),
			tag.ClientError(err),
		)
		err = fakeErr
		return
	}
	return
}
