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/matching"
	"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 (
	msgMatchingInjectedFakeErr = "Injected fake matching client error"
)

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

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

func (c *matchingClient) AddActivityTask(ctx context.Context, ap1 *types.AddActivityTaskRequest, p1 ...yarpc.CallOption) (ap2 *types.AddActivityTaskResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		ap2, err = c.client.AddActivityTask(ctx, ap1, p1...)
	}

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

func (c *matchingClient) AddDecisionTask(ctx context.Context, ap1 *types.AddDecisionTaskRequest, p1 ...yarpc.CallOption) (ap2 *types.AddDecisionTaskResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		ap2, err = c.client.AddDecisionTask(ctx, ap1, p1...)
	}

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

func (c *matchingClient) CancelOutstandingPoll(ctx context.Context, cp1 *types.CancelOutstandingPollRequest, p1 ...yarpc.CallOption) (err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		err = c.client.CancelOutstandingPoll(ctx, cp1, p1...)
	}

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

func (c *matchingClient) DescribeTaskList(ctx context.Context, mp1 *types.MatchingDescribeTaskListRequest, p1 ...yarpc.CallOption) (dp1 *types.DescribeTaskListResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		dp1, err = c.client.DescribeTaskList(ctx, mp1, p1...)
	}

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

func (c *matchingClient) GetTaskListsByDomain(ctx context.Context, gp1 *types.GetTaskListsByDomainRequest, p1 ...yarpc.CallOption) (gp2 *types.GetTaskListsByDomainResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		gp2, err = c.client.GetTaskListsByDomain(ctx, gp1, p1...)
	}

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

func (c *matchingClient) ListTaskListPartitions(ctx context.Context, mp1 *types.MatchingListTaskListPartitionsRequest, p1 ...yarpc.CallOption) (lp1 *types.ListTaskListPartitionsResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		lp1, err = c.client.ListTaskListPartitions(ctx, mp1, p1...)
	}

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

func (c *matchingClient) PollForActivityTask(ctx context.Context, mp1 *types.MatchingPollForActivityTaskRequest, p1 ...yarpc.CallOption) (mp2 *types.MatchingPollForActivityTaskResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		mp2, err = c.client.PollForActivityTask(ctx, mp1, p1...)
	}

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

func (c *matchingClient) PollForDecisionTask(ctx context.Context, mp1 *types.MatchingPollForDecisionTaskRequest, p1 ...yarpc.CallOption) (mp2 *types.MatchingPollForDecisionTaskResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		mp2, err = c.client.PollForDecisionTask(ctx, mp1, p1...)
	}

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

func (c *matchingClient) QueryWorkflow(ctx context.Context, mp1 *types.MatchingQueryWorkflowRequest, p1 ...yarpc.CallOption) (mp2 *types.MatchingQueryWorkflowResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		mp2, err = c.client.QueryWorkflow(ctx, mp1, p1...)
	}

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

func (c *matchingClient) RefreshTaskListPartitionConfig(ctx context.Context, mp1 *types.MatchingRefreshTaskListPartitionConfigRequest, p1 ...yarpc.CallOption) (mp2 *types.MatchingRefreshTaskListPartitionConfigResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		mp2, err = c.client.RefreshTaskListPartitionConfig(ctx, mp1, p1...)
	}

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

func (c *matchingClient) RespondQueryTaskCompleted(ctx context.Context, mp1 *types.MatchingRespondQueryTaskCompletedRequest, p1 ...yarpc.CallOption) (err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		err = c.client.RespondQueryTaskCompleted(ctx, mp1, p1...)
	}

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

func (c *matchingClient) UpdateTaskListPartitionConfig(ctx context.Context, mp1 *types.MatchingUpdateTaskListPartitionConfigRequest, p1 ...yarpc.CallOption) (mp2 *types.MatchingUpdateTaskListPartitionConfigResponse, err error) {
	fakeErr := c.fakeErrFn(c.errorRate)
	var forwardCall bool
	if forwardCall = c.forwardCallFn(fakeErr); forwardCall {
		mp2, err = c.client.UpdateTaskListPartitionConfig(ctx, mp1, p1...)
	}

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