Debugging eval() errors inside a loop

I have a large database that I read from a yaml file. I loop over elements of the database to create the document text. I call eval on items from the database because the strings actually contain Typst function calls.

The problem is that if there is an error in eval I can not see which of the loop iterations/database items that cause the error. The App does show the first 10 variable values in the loop but that doesn’t help since there are 100s of loop iterations before the error.

Any ideas how to debug this?

Here is a code snippet:

for (field) in regdb.at("Fields") {
       let desc = field.at("Description")
       let ev_desc = eval(desc, mode:"markup",
          scope:(def:def,tab:tab,register:register,field:field))

You can select the indices to loop over manually using Array Type – Typst Documentation instead of looping over all fields. Alternatively you could (temporarily) delete fields from the yaml file until you find the problematic one(s).

Good idea. I’m looping over a dictionary but I could add a counter in the loop so I can break the loop at a specific index. What I did before was what you suggested, to delete fields in the yaml file, interval halfing, but that is very cumbersome.

I see, in that case I would grab the dictionary keys and loop over them using keys.slice(start, stop). This is more flexible than the counter in the loop since you can move in batches of e.g. 100 over the dictionary.

1 Like