Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- refactor(agent): extract `readCertifiedReject` helper to deduplicate certified reject logic
- refactor(agent): make `CallContext.httpDetails` optional for polling error paths
- feat(agent): add `rawCertificate` to `pollForResponse` return type and export `PollForResponseResult`
- fix(candid): improve error messages for candid decoding errors in Variant types

## [5.0.0] - 2025-12-18

Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/candid/idl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,15 @@ test('decode / encode unknown variant', () => {
expect(reencoded).toEqual(encoded);
});

test("decode variant with unknown field hash throws descriptive error", () => {
// Encode {ok: "good"} with variant {ok: Text, err: Text}
const encoded = hexToBytes("4449444c016b029cc20171e58eb4027101000004676f6f64");
// Decode with a variant that does not include "ok" or "err"
const MismatchedVariant = IDL.Variant({ foo: IDL.Text });
expect(() => IDL.decode([MismatchedVariant], encoded)).toThrow(/Cannot find field hash/);
expect(() => IDL.decode([MismatchedVariant], encoded)).toThrow(/expected fields: \[foo\]/);
});

test('throw on serializing unknown', () => {
expect(() => IDL.encode([IDL.Unknown], ['test'])).toThrow('Unknown cannot be serialized');
});
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/candid/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,10 @@ export class VariantClass extends ConstructType<Record<string, any>> {
return { [key]: value };
}
}
throw new Error(`Cannot find field hash ${wireHash}`);
const expectedFields = this._fields.map(([name]) => name).join(', ');
throw new Error(
`Cannot find field hash ${wireHash} in variant, expected fields: [${expectedFields}]`,
);
}

get name() {
Expand Down
Loading