Skip to content
CCPEDIAby Unity Nodes
App Development

Catching failed key lookup

2 posts220 views1 likesLast activity Feb 2022

  1. LucianoOP

    If I run this code, which creates a t if it doesn’t exist already:

    lookupOrCreate : (HasCreate t, HasFetchByKey t t)  => t  -> Update t
    lookupOrCreate = 
      try fst <$> fetchByKey t
      catch _ -> create t
    

    The catch will not trigger, and I get an error

    Attempt to fetch or exercise by key but no contract with that key was found.
    

    I suppose I can work around this by using lookupByKey, but I’m curious to understand why this exception is not captured?

  2. cocreature 1

    If you allow catching this, you turn a fetchByKey into a lookupByKey. However, lookupByKey and fetchByKey have different authorization and visibility rules, see Contract keys — Daml SDK 1.18.1 documentation so that doesn’t really work.