How to handle error catching within a typst document?

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 :)