How to handle error catching within a typst document?

My particular use case is inserting images from a list, if they exists, if they don’t exist then do nothing.

#let list = ("A","B","C")

#for v in list [
  // how to catch and ignore error if image do not exist (path is invalid)
  #image("graphic/" + v + ".jpg")
]

I have tried to play around with #assert and #panic but to no avail.

Thanks in advance for the help! It is really appreciated!

1 Like

Hey,
As far as I know, this is not possible.

Maybe you can try this snippet: Try & Catch - Typst Examples Book (please note the “don’t try this at home”)

A solution is to do the verifying yourself or with a script beforehand and pass the valid files via the --input argument to the Typst file (docs: System Functions – Typst Documentation)


I highly recommend to only use assets which are available to you in a Typst project either from yourself or from packages, instead of relying on a try-catch basis. Meaning, if you provide content through a script or similar, name it accordingly and work with the --input parameter provided by the Typst app.

Typst at the end of the day is a type-setting system.

1 Like

Can I add a similar plea, and give a usage case?

I want to be able to print flags. The data is coming from a database that has ISO 3166-1 2-alpha country codes, so

#flag("EU")

works just fine - EU is on the 3166-1 list as a “reserved” code, and is recognised by FLAGADA package.

But

#flag("CQ")

crashes my entire document – CQ is a reserved 3166-1 code, but not recognised by the FLAGADA package.

This means that to successfully automate a database to publication pipeline, I need to either:

– ensure my terrible Python code can pre-vet that country codes on the database are included in the FLAGADA package;

or

– exhort my users to never enter a country code that is not on the FLAGADA package list.

So I’d be very happy to emit Typst code that looks something like this:

#TRY(#flag("CQ")){#figure(image: "no-flag.png")}

Everything would work like magic!

And Typst would be a step closer to being a robust type-setting system :)

Hello @terasa Could you please post a MWE that contains the code that crashed your document?

Sorry only just seen your message. If I’d got a notification that I needed to do something, I must have missed it.

Two line document:

#import "@preview/flagada:1.0.1" : *
#flag("CQ")

Command line compile, Windows, Version typst 0.14.2 (b33de9de)

typst c flag-test.typ
error: dictionary does not contain key "CQ" and no default value was specified
     ┌─ @preview/flagada:1.0.1\flags.typ:8279:2
     │
8279 │   flags.at(upper(iso3166))
     │   ^^^^^^^^^^^^^^^^^^^^^^^^

help: error occurred in this call of function `flag`
  ┌─ \\?\(file name)
  │
2 │ #flag("CQ")
  │  ^^^^^^^^^^

I see nothing in the FLAGDA documentation about providing a default flag name.

Thanks!

The error is because the flag function is defined as

#let flag(iso3166, height:.65em,) = {
  assert(iso3166.len()==2 and type(iso3166)==str,message: "iso3166 code should be a string of 2 letters")
  let flags = (
    AD: flag-ad(height:height),
    AE: flag-ae(height:height),
    // ...
    // 200 lines ommitted
    // ...
    ZW: flag-zw(height:height),
  )
  flags.at(upper(iso3166))
}

So it basically defines a dictionary called flags and tries to fetch the value at the key given by the user. The error is the same one you would get when attempting to access a non-existent key in any dictionary (see docs):

#("testing":"test").at("test")

Unfortunately, there isn’t really a way to deal with this using just typst. I would copy the files locally (see How to store and use imported templates locally? - #2 by aarnent), implement the required changes there (like support for more flags, error handing) and open a pull request on the repo: GitHub - samrenault/flagada: A Typst package to generate countries flags, selecting country based on its ISO3166-1 code · GitHub.
While you wait for it to get merged, you can use your local version of the package :)