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"
)

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

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

func (c *injectorHistoryManager) AppendHistoryNodes(ctx context.Context, request *persistence.AppendHistoryNodesRequest) (ap1 *persistence.AppendHistoryNodesResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		ap1, err = c.wrapped.AppendHistoryNodes(ctx, request)
	}

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

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

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

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

func (c *injectorHistoryManager) ForkHistoryBranch(ctx context.Context, request *persistence.ForkHistoryBranchRequest) (fp1 *persistence.ForkHistoryBranchResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		fp1, err = c.wrapped.ForkHistoryBranch(ctx, request)
	}

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

func (c *injectorHistoryManager) GetAllHistoryTreeBranches(ctx context.Context, request *persistence.GetAllHistoryTreeBranchesRequest) (gp1 *persistence.GetAllHistoryTreeBranchesResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		gp1, err = c.wrapped.GetAllHistoryTreeBranches(ctx, request)
	}

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

func (c *injectorHistoryManager) GetHistoryTree(ctx context.Context, request *persistence.GetHistoryTreeRequest) (gp1 *persistence.GetHistoryTreeResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		gp1, err = c.wrapped.GetHistoryTree(ctx, request)
	}

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

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

func (c *injectorHistoryManager) ReadHistoryBranch(ctx context.Context, request *persistence.ReadHistoryBranchRequest) (rp1 *persistence.ReadHistoryBranchResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		rp1, err = c.wrapped.ReadHistoryBranch(ctx, request)
	}

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

func (c *injectorHistoryManager) ReadHistoryBranchByBatch(ctx context.Context, request *persistence.ReadHistoryBranchRequest) (rp1 *persistence.ReadHistoryBranchByBatchResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		rp1, err = c.wrapped.ReadHistoryBranchByBatch(ctx, request)
	}

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

func (c *injectorHistoryManager) ReadRawHistoryBranch(ctx context.Context, request *persistence.ReadHistoryBranchRequest) (rp1 *persistence.ReadRawHistoryBranchResponse, err error) {
	fakeErr := generateFakeError(c.errorRate)
	var forwardCall bool
	if forwardCall = shouldForwardCallToPersistence(fakeErr); forwardCall {
		rp1, err = c.wrapped.ReadRawHistoryBranch(ctx, request)
	}

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