How to debug native errors? - Answered

I am having some challenges debugging this error.
The error only shows up during a test. No Problems mentioned in vs code.
Am I missing something obvious? The error occurs in borrow_child_object.

┌── test_user_journey_1 ──────
│ error[E11001]: test failure
│     ┌─ /..../sui-framework/packages/sui-framework/sources/dynamic_field.move:217:31
│     │
│ 217 │     public(friend) native fun borrow_child_object<Child: key>(object: &UID, id: address): &Child;
│     │                               ^^^^^^^^^^^^^^^^^^^
│     │                               │
│     │                               Test was not expected to error, but it aborted with code 1 originating in the module 0x2::dynamic_field rooted here
│     │                               In this function in 0x2::dynamic_field
│ stack trace
│  table::borrow(.../table.move:54)


let votes_given = table::borrow(              
1 Like

Answer from Damir on telegram:
One tip at debugging most of the native errors. We usually add error constants in modules to mark abort codes.

As you can see from the codes descriptions, 1 stands for missing dynamic field, which means that you’re tying to borrow a field that doesn’t exist.

Generally we recommend adding an existence check for every borrow / borrow_mut / remove you have and choose and add a custom abort code, so you don’t get this error in a function 100 lines long which calls other functions and modules and so on.

1 Like

The solution ended up being quite straightforward. Knowing now that the 1 means EFieldDoesNotExist.
I figured that a check was required for the existence of the key, and adding the key if required.

// check if the voter_addr exists in the votes_by_user_counters
if ( !table::contains(&proposal_register.votes_by_user_counters, voter_addr) ) {
   // if not, add the voter_addr to the votes_by_user_counters
   table::add(&mut proposal_register.votes_by_user_counters, voter_addr, 0);