Skip to content

chore(cfn): classify StackDeployer failures with sentinels, not substrings (#502) - #536

Merged
scttfrdmn merged 1 commit into
mainfrom
chore/502-typed-deployer-errors
Aug 3, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
chore/502-typed-deployer-errors

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

cfnMapDeployerError recovered a CloudFormation error code and HTTP status by
running strings.Contains over the deployer's message. That worked, and it coupled
two things that have no business being coupled.

It was fragile in both directions. Rewording stack %q not found — an ordinary
copy-edit — silently turned a ValidationError at 400 into an InternalFailure at
500 for every consumer, with no compiler error and no failing test outside the
plugin's own suite. And it was lossy the other way: deploy resource %s: %w wraps
whatever the plugin returned, and a plugin's own message may well contain
"not found" — an instance whose AMI does not resolve, say — so a genuine
resource-level failure was reported as though the request had named an absent
stack, at 400 rather than 500.

What changed

StackDeployer wraps one of six sentinels — ErrCFNStackNotFound,
ErrCFNChangeSetNotFound, ErrCFNDriftDetectionNotFound, ErrCFNTemplateInvalid,
ErrCFNResourceDeployFailed, ErrCFNStateRequired — and the plugin classifies with
errors.Is. No strings.Contains remains.

Every message is unchanged byte for byte. That is what makes this safe to land
on its own: anything reading a substrate log sees exactly what it saw before, and
only the classification is new. The classification is held in a field on
cfnClassifiedError rather than wrapped into the text with a second %w,
precisely so text and code stay independent — wrapping the sentinel into the
message would have satisfied errors.Is and changed the text every consumer's logs
already carry, which is the coupling the issue is about, reintroduced from the other
end.

A failed resource additionally carries its logical ID on a typed
CFNResourceDeployError. DescribeStackResources reads a failure off the resource
record rather than off this error (#519), but a deploy that fails outright returns
before any record is written, so the logical ID has to travel with the error or it
is lost.

Why now

The v0.88.0 plan parked #502 with a stated trigger: it joins the release if
#522 surfaces its new failures through Deploy/DeleteStack's error return rather
than on a resource. #522's remaining half is Fn::ImportValue, whose delete-refusal
("you can't delete the stack that is exporting the output value") has no resource to
blame and so must come back through DeleteStack. The trigger fired, so this lands
first as a pure mechanical refactor and the export work builds on typed errors
instead of widening the substring switch.

Tests

emulator/betty_cfn_errors_test.go. Two assertions carry the weight:

  • The message is unchanged. Every not-found path asserts err.Error() against
    the exact string it produced before, byte for byte.
  • The code does not depend on the wording. Six cases hand the mapping a message
    sharing no word with the old switch's strings (no stack named "x" exists,
    the body could not be decoded as JSON or YAML) and assert the code and status
    still resolve. Under the old mapping every one falls through to InternalFailure.

Plus: the parse failure's *json.SyntaxError is still reachable through errors.As
underneath the classification; a resource failure whose cause reads "not found" is
NotErrorIs a missing stack; and the deploy-resource path is provoked through a
real template (a YAML mapping with a non-string key decodes fine and then cannot
be JSON-marshaled into a plugin request) rather than by building the error, so the
wrapping at the call site is asserted too.

Mutation testing

18 mutants: 18 CAUGHT, 0 SURVIVED, 0 EQUIVALENT. Anchor-count assert before
applying, gofmt -w && go vet gate, -race.

Three survived the first pass and each was a real test-design gap, closed rather
than proved equivalent:

The highest-value mutant, map-falls-back-to-substring-match, restores the exact
pre-#502 switch and is caught by three tests.

Verification

gofmt -l ., go build ./..., go vet ./..., golangci-lint run ./... (0 issues,
unpiped), make test (race), make docs-reference docs-reference-check docs-versions, npm --prefix docs run build, go vet -C test/e2e ./...,
go test -C test/e2e ./... — all green.

Docs: docs/services.md §CloudFormation now states the code/status a consumer gets
for each class of failure, and that it is derived from the classification rather
than the message. CHANGELOG under ## [Unreleased] → ### Changed.

Closes #502

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.87097% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/betty_cfn.go 82.75% 5 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 79ba8c6 into main Aug 3, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the chore/502-typed-deployer-errors branch August 3, 2026 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CloudFormation: StackDeployer errors are untyped, so the wire plugin maps codes by string matching

1 participant