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

     1  # Tests of Starlark 'errors' extension.
     2  load("errors.star", "errors")
     3  
     4  err_msg = "hello"
     5  err_val = errors.error("custom error [%s]" % (err_msg))
     6  print(err_val)
     7  
     8  def failing_func():
     9      fail("something went wrong")
    10  
    11  r1 = errors.catch(failing_func)
    12  
    13  def test_catch(t):
    14      t.true(not r1)
    15      t.true(r1.err)  # error is truthy
    16      t.true(r1.err.matches(".* wrong"))
    17  
    18  def access_value():
    19      return r1.val
    20  
    21  # trying to access the value fails with the original error
    22  def test_fails(t):
    23      t.fails(access_value, "something went wrong")
    24  
    25  def hello(name):
    26      return "hello, " + name
    27  
    28  def test_errors(t):
    29      r2 = errors.catch(hello, "world")
    30      t.true(r2)
    31      t.true(not r2.err)
    32  
    33      t.eq(r2.val, "hello, world")
    34  
    35      r3 = errors.catch(io_eof_func)
    36      t.true(r3.err)
    37  
    38      # check error is type of io.EOF error
    39      t.true(r3.err.kind(io_eof))
    40  
    41      # TODO: handle extracting error values
    42      #path_err = r4.err.as(io_path_err)
    43      #if path_err:
    44      #    print("got path error")