Catching failed key lookup

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?

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.

1 Like