package errorinjectors

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

import (
	"context"

	"github.com/uber/cadence/common/log"
	"github.com/uber/cadence/common/persistence"
)

// injectorShardManager implements persistence.ShardManager interface instrumented with error injection.
type injectorShardManager struct {
	wrapped   persistence.ShardManager
	errorRate float64
	logger    log.Logger
}

// NewShardManager creates a new instance of ShardManager with error injection.
func NewShardManager(
	wrapped persistence.ShardManager,
	errorRate float64,
	logger log.Logger,
) persistence.ShardManager {
	return &injectorShardManager{
		wrapped:   wrapped,
		errorRate: errorRate,
		logger:    logger,
	}
}

func (c *injectorShardManager) Close() {
	c.wrapped.Close()
	return
}

func (c *injectorShardManager) CreateShard(ctx context.Context, request *persistence.CreateShardRequest) (err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		err = c.wrapped.CreateShard(ctx, request)
	}

	if fakeErr != nil {
		logErr(c.logger, "ShardManager.CreateShard", fakeErr, forwardCall, err)
		err = fakeErr
		return
	}
	return
}

func (c *injectorShardManager) GetName() (s1 string) {
	return c.wrapped.GetName()
}

func (c *injectorShardManager) GetShard(ctx context.Context, request *persistence.GetShardRequest) (gp1 *persistence.GetShardResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		gp1, err = c.wrapped.GetShard(ctx, request)
	}

	if fakeErr != nil {
		logErr(c.logger, "ShardManager.GetShard", fakeErr, forwardCall, err)
		err = fakeErr
		return
	}
	return
}

func (c *injectorShardManager) UpdateShard(ctx context.Context, request *persistence.UpdateShardRequest) (err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		err = c.wrapped.UpdateShard(ctx, request)
	}

	if fakeErr != nil {
		logErr(c.logger, "ShardManager.UpdateShard", fakeErr, forwardCall, err)
		err = fakeErr
		return
	}
	return
}
