github.com/emcfarlane/larking@v0.0.0-20220605172417-1704b45ee6c3/starlib/starlarkerrors/testdata/example_errors.star (about)

     1  load("errors.star", "errors")
     2  
     3  def assert_even(x):
     4      if x % 2 == 0:
     5          fail("odd")
     6      return x
     7  
     8  def test_catch(t):
     9      # catch the error and check the result.
    10      # Results will be truthy if valid, and falsy if not.
    11      # Okay values can be accessed by the .val attribute.
    12      res = errors.catch(assert_even, 2)
    13      if res:
    14          print("% is even!", res.val)
    15  
    16      # Error values can be accessed by the .err attribute.
    17      res = errors.catch(assert_even, 3)
    18      if not res:
    19          print("error: %", res.err)