fix: parse StructuredError details array in 465 error handlers#229
Open
fix: parse StructuredError details array in 465 error handlers#229
Conversation
The replicate command was using getCvmComposeConfig which calls
/cvms/{id}/compose_file — an endpoint that does not return env_pubkey.
This caused "undefined is not an object" when encrypting env vars.
Switch to safeGetCvmInfo + getEncryptPubkey, matching the approach
used by `phala env encrypt`. Remove the unused getCvmComposeConfig
helper and CvmComposeConfigResponse type.
- Add --compose-hash, --prepare-only, --commit, --token, --transaction-hash options
- Rename --teepod-id to --node-id with name resolution support
- Handle 465 responses for on-chain KMS prepare flow
- Add commit command hints to prepare-only output
- Use instance-level /cvms/{vm_uuid}/replicas endpoint
- Show Team (from source CVM workspace) and App URL in replica output - Keep CVM UUID dashes intact (don't strip hyphens) - Show KMS type in output - Add 0x prefix to compose hash, app id, device id in prepare output - Preserve ResourceError from safeGetCvmInfo so error codes (e.g. ERR-03-003, ERR-03-009) and suggestions are displayed
Contributor
📋 Check Results✨ JS SDK - Code FormattingShow format check results🔍 JS SDK - TypeScript Type CheckShow type check output🧪 JS SDK - Test ResultsShow test output📝 JS SDK - Lint CheckShow lint results🌐 JS SDK - Browser CompatibilityShow browser test results🌐 Browser Compatibility ReportBrowser compatibility tests completed across:
The SDK has been verified to work in modern browser environments. Check run: https://github.com/Phala-Network/phala-cloud/actions/runs/23946402732 |
The 465 error handlers in updateCvmEnvs, updatePrelaunchScript, and
updateDockerCompose expected flat fields on the error detail object
(e.g. detail.compose_hash), but the backend returns StructuredError
format with a details array:
{ details: [{ field: "compose_hash", value: "..." }, ...] }
Extract fields from the details array using the same pattern already
used in patchCvm's extractDetailsMap function. This fixes the two-phase
on-chain signing flow for contract-owned KMS CVMs.
RequestError.fromFetchError set detail to parseResult.data.detail, which is undefined for StructuredError responses (they use 'details' array, not 'detail'). This caused the original response body to be lost when converting FetchError → RequestError → BusinessError. When detail is undefined but error.data contains a StructuredError (has error_code), fall back to using error.data as detail. This allows parseStructuredError in parseApiError to correctly parse it and return a ResourceError with the full structured data. Also remove debug console.log from updateCvmEnvs.
- errors.test.ts: verify RequestError.fromFetchError preserves StructuredError response body as detail, and parseApiError produces ResourceError with correct fields - update_cvm_envs.test.ts: verify 465 → precondition_required conversion with compose_hash, app_id, kms_info extraction
216da9e to
c6e16ef
Compare
Comment on lines
+231
to
+241
| if (composeHash && appId) { | ||
| return { | ||
| status: "precondition_required" as const, | ||
| message: (detailObj.message as string) || "Compose hash verification required", | ||
| compose_hash: composeHash as string, | ||
| app_id: appId as string, | ||
| device_id: (deviceId as string) || "", | ||
| kms_info: kmsInfo, | ||
| }; | ||
| } | ||
| } |
There was a problem hiding this comment.
Bug: The logic for handling 465 errors checks for composeHash and appId but not kmsInfo, leading to a schema validation failure if kmsInfo is missing from the error details.
Severity: MEDIUM
Suggested Fix
To fix the logical inconsistency, either make the kms_info field optional in the Zod schema (e.g., kms_info: KmsInfoSchema.optional()) or add a check for kmsInfo in the conditional statement to ensure it exists before proceeding (e.g., if (composeHash && appId && kmsInfo)).
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: js/src/actions/cvms/update_cvm_envs.ts#L231-L241
Potential issue: In the error handling for a 465 status, the code extracts
`composeHash`, `appId`, and `kmsInfo` from the error details. It then proceeds if
`composeHash` and `appId` are present, but it does not check for the existence of
`kmsInfo`. The resulting object is then validated against a schema where the `kms_info`
field is mandatory. If the backend returns a 465 error that includes `composeHash` and
`appId` but omits `kms_info`, the code will attempt to build an object with an undefined
`kms_info`. This will cause a Zod schema validation error, throwing an exception instead
of correctly returning the `precondition_required` status and breaking the intended
two-phase update flow.
Did we get this right? 👍 / 👎 to inform future reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
updateCvmEnvs,updatePrelaunchScript, andupdateDockerComposeto correctly parse StructuredError format{ details: [{ field: "compose_hash", value: "..." }, ...] }but SDK expected flatdetail.compose_hashpatchCvm'sextractDetailsMapTest Plan
precondition_requiredinstead of throwing