dfinity / agent-js

A collection of libraries and tools for building software around the Internet Computer, in JavaScript.
https://agent-js.icp.xyz
Apache License 2.0
150 stars 95 forks source link

Improve error messages for candid decoding errors #470

Open FloorLamp opened 3 years ago

FloorLamp commented 3 years ago

Is your feature request related to a problem? Please describe. This is an issue with @dfinity/candid.

In general, candid decoding errors only throw an error and are not very descriptive.

Example This is an example of a candid interface with missing variants.

const Service = ({ IDL }: any) =>
  IDL.Service({
    get_full_neuron: IDL.Func(
      [IDL.Nat64],
      [IDL.Variant({ Invalid: IDL.Null })],
      ["query"]
    ),
  });

const agent = new HttpAgent({ host: "https://ic0.app" });
const governance = Actor.createActor(Service, {
  agent,
  canisterId: "rrkah-fqaaa-aaaaa-aaaaq-cai",
});

(async () => {
  await governance.get_full_neuron(1);
})();

This will only throw an error, seemingly without any obvious resolution.

Error: Cannot find field hash _3456837_
    at VariantClass.decodeValue (.../@dfinity/candid/src/idl.ts:1018:11)

Describe the solution you'd like One way to improve errors is to print the raw candid response along with our type:

Error: Unexpected field hash _3456837_. Did you forget to include a variant?

Received raw candid: 
[
  {
    "_3456837_": {
      "_1389388560_": "",
      "_3790638545_": 3
    }
  }
]

Provided type:
variant {Invalid:null}
krpeacock commented 2 years ago

Thanks, this seems reasonably scoped! I'll add it to my backlog