Skip to content

Add error handling function to match error codes in nested API errorsΒ #59

Open
@gdavison

Description

Description

Some AWS API errors return a fairly generic error code with a nested specific code. Currently we only check the top-level error code, and sometimes check the error message. AWS strongly discourages checking the error message, since it can change.

Using the current functions, this is how a specific error message can be checked for

if tfawserr.ErrCodeEquals(err, storagegateway.ErrCodeInvalidGatewayRequestException) {
	var awsErr awserr.Error
	if errors.As(err, &awsErr) {
		nestedErr := awsErr.OrigErr()
		if nestedErr != nil && tfawserr.ErrCodeEquals(nestedErr, "FileSystemAssociationNotFound") {
			return nil, FsxFileSystemStatusNotFound, nil
		}
	}
}

Potential Library Implementation

func ErrCodeEqualsNested(err error, code string) (awserr.Error, bool)

The function should loop until the error code matches or err.OrigErr() returns nil. The awserr.Error return value will be the matched error or nil. The bool return value will be true if there is a match.

References

hashicorp/terraform-provider-aws#20082 (comment)

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions