Skip to content

fix(ec2): serialize errors in the ec2 protocol's own document (#591) - #592

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/591-ec2-error-shape
Aug 9, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/591-ec2-error-shape

Conversation

@scttfrdmn

Copy link
Copy Markdown
Owner

Closes #591

EC2 is not on the AWS Query protocol. It is the only service on the ec2 protocol, whose error document wraps the error in a plural <Errors> element and spells the request id <RequestID> with a capital D. Substrate served the Query shape, so AWS SDK v2's ec2query deserializer — which reads the code at the XPath Errors>Error>Code — recovered an empty string and fell back to smithy.GenericAPIError{Code: "UnknownError"}. A consumer branching on InsufficientInstanceCapacity or InvalidInstanceID.NotFound never saw either code, so every EC2 retry/wait loop written against the SDK was untestable.

Organic errors were broken too, not just faults

The issue reported a seeded capacity fault and predicted the rest; this confirms it. writeError has 8 call sites — parse, signature, presigned expiry, auth, quota, consistency, fault, and the plugin route — and all of them delegate the shape to one marshalAWSError call, so DescribeInstances on an absent instance ID arrived as UnknownError exactly as the injected fault did. Both are fixed by the single arm; no server.go change was needed.

Why an arm and not a wider default

The two documents are mutually exclusive: the Query parser reads Error>Code where the EC2 parser reads Errors>Error>Code, and each recovers "" from the other's document (asserted in TestEC2Error_IsNotTheQueryDocument). So EC2 needed its own arm — following the structure #480 established for S3 — and IAM, CloudFormation and every other XML service keep the wrapper they had. ec2 is the sole member of the new arm, checked against the protocol field of every botocore service model.

The <RequestID> is the fixed string SUBSTRATE, matching S3's document rather than generateRequestID(), which derives from time.Now().UnixNano() and would make an error body differ between two replays of one recorded run. That literal became a shared substrateRequestID constant so the reasoning lives in one place.

Why substrate's own coverage missed this

botocore is lenient where the SDKs are strict. EC2QueryParser._get_error_root walks the root's children for Errors and returns original_root when it is absent, so the AWS CLI read the correct code out of the wrong document the entire time — verified live, aws ec2 describe-instances reported InvalidInstanceID.NotFound even before this change. Every CLI-driven EC2 test passed, and no unit test asserting bytes would have flagged the shape as wrong without knowing the SDK's XPath.

So the regression gate is a real-SDK one: test/e2e/journey_ec2_errors_test.go asserts smithy.APIError.ErrorCode() for both the organic and the injected case, and test/e2e now depends on aws-sdk-go-v2/service/ec2 to host it. Reverting the one map entry makes it fail with ErrorCode() = "UnknownError", which is #591 verbatim.

Two existing tests encoded the false premise in their names and moved rather than being weakened: TestQueryXMLError_UsesErrorDocument (the #392 gate, which used EC2 as its vehicle for the generic Query shape) and TestFault_OtherProtocolsAreUnchanged's "ec2 stays query xml" subtest. Both now use CloudFormation, which is genuinely Query, and EC2 got its own subtest. Loosening either to accept both shapes would have retired the gate instead of moving it.

Verification

  • gofmt -l . clean, go build ./..., go vet ./..., golangci-lint run ./... → 0 issues; make test (race) green in both modules. The 4 lint findings in test/e2e/e2e_test.go are pre-existing and untouched; the new file is clean.
  • Three EC2 error-decode helpers moved to the SDK's XPath (ec2ErrorCode, ec2ErrorDetail, ec2ErrorCodeOf), which puts the existing EC2 suite behind the shape: reverting the map entry fails 159 unit assertions plus the e2e gate. The third helper was found by the suite, not by grep — it omits the XMLName field.
  • Live against substrate server: EC2 returns <Response><Errors><Error><Code>… with <RequestID>SUBSTRATE</RequestID> on both the organic and injected paths; IAM and CloudFormation still return <ErrorResponse><Error><Type>Sender</Type>…; the AWS CLI reads both the organic and the injected code correctly (no regression); a CFN stack holding an AWS::EC2::VPC still reaches CREATE_COMPLETE, since cfnDispatchError reads the code with extractXMLField — a substring scan at any depth, not a struct decode.
  • Mutation testing, 5 mutants, all CAUGHT, none surviving: ec2 reverted to the Query arm (159 unit + e2e); the <Errors> wrapper dropped (159 unit + e2e); <RequestID> respelled <RequestId> (unit only — the SDK does not need the request id to recover the code, which is why that spelling got its own explicit assertion); the arm applied beyond ec2 (the CFN suite goes red, proving the carve-out is scoped); and RequestID switched to generateRequestID() (the determinism gate).

Provenance

The document shape is quoted from the EC2 error reference; the XPaths come from aws-sdk-go-v2/aws/protocol/ec2query/error_utils.go. RequestID: "SUBSTRATE" is substrate's own choice for replay determinism, not a value AWS sends.

Compatibility

Any test decoding an EC2 error as <ErrorResponse><Error> must switch to <Response><Errors><Error>. The AWS CLI and the SDKs are unaffected in the sense that neither breaks — but an SDK caller now gets a correct code where it previously got UnknownError, so an error-matching branch that silently never fired may start firing.

EC2 is the only service on the "ec2" protocol, whose error document wraps the
error in a plural <Errors> element and spells the request id <RequestID> with a
capital D. Substrate served the Query protocol's <ErrorResponse><Error>, so
aws-sdk-go-v2's ec2query deserializer — which reads the code at the XPath
Errors>Error>Code — recovered an empty string and fell back to UnknownError.

Organic errors were affected as much as injected faults: both share writeError's
single serializer, so DescribeInstances on an absent instance ID arrived as
UnknownError too. The issue reported the fault and predicted the rest.

EC2 gets its own error-protocol arm, alongside the one S3 got for the same class
of bug in #480, and it is the sole member. The two documents are mutually
exclusive — the Query parser reads Error>Code where the EC2 parser reads
Errors>Error>Code — so this could not be a shared shape.

The regression gate is a real-SDK one. botocore is lenient where the SDKs are
strict: its EC2 parser falls back to the document root when <Errors> is absent,
so the AWS CLI read the correct code out of the wrong document and every
CLI-driven test passed. test/e2e now depends on aws-sdk-go-v2/service/ec2 to
host that gate.
@codecov

codecov Bot commented Aug 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
emulator/error_protocol.go 92.59% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit 0859a2e into main Aug 9, 2026
16 checks passed
@scttfrdmn
scttfrdmn deleted the fix/591-ec2-error-shape branch August 9, 2026 17:48
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.

EC2 plugin's injected/real errors use <ErrorResponse> shape; AWS SDK v2's ec2query deserializer expects <Errors><Error> wrapper

1 participant