// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
//	http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package networkinterface

import (
	"fmt"

	"github.com/pkg/errors"
)

// UnableToFindENIError is an error type that is used to handle cases where
// the ENI device cannot be found, even after it has been acknowledged as
// "attached" by the agent. It lets us special case this error in dispatcher
// and task director workflows.
type UnableToFindENIError struct {
	macAddress          string
	associationProtocol string
}

// NewUnableToFindENIError creates a new UnableToFindENIError object.
func NewUnableToFindENIError(macAddress, associationProtocol string) error {
	return &UnableToFindENIError{
		macAddress:          macAddress,
		associationProtocol: associationProtocol,
	}
}

func (e *UnableToFindENIError) Error() string {
	return fmt.Sprintf("unable to find device name for '%s' eni %s",
		e.associationProtocol, e.macAddress)
}

// IsUnableToFindENIError returns true if the error type is of type
// `UnableToFindENIError`.
func IsUnableToFindENIError(err error) bool {
	_, ok := errors.Cause(err).(*UnableToFindENIError)
	return ok
}
