title_body: Plutus Pioneer Program - Lecture #4 Homework - Best practice for logging errors Below is my attempt to implement the homework assignment.
  Is the way I call the endpoint twice in payTrace with minor differences appropriate?
  Since payTest2 would not execute properly without modifying the original payContract, is the way I handle the contract error appropriate?
  payContract :: Contract () PaySchema Text ()
  payContract = do
      pp &lt;- endpoint @&quot;pay&quot;
      let tx = mustPayToPubKey (ppRecipient pp) $ lovelaceValueOf $ ppLovelace pp
      Contract.handleError (\err -&gt; Contract.logError $ &quot;Caught error: &quot; ++ unpack err) $ (void $ submitTx tx)
      payContract
  
  -- A trace that invokes the pay endpoint of payContract on Wallet 1 twice, each time with Wallet 2 as
  -- recipient, but with amounts given by the two arguments. There should be a delay of one slot
  -- after each endpoint call.
  payTrace :: Integer -&gt; Integer -&gt; EmulatorTrace ()
  payTrace x y = do
      h1 &lt;- activateContractWallet (Wallet 1) payContract
      callEndpoint @&quot;pay&quot; h1 $ PayParams
          { ppRecipient = pubKeyHash $ walletPubKey $ Wallet 2
          , ppLovelace  = x
          }
      void $ Emulator.waitNSlots 1
      xs &lt;- observableState h1
      Extras.logInfo $ show xs
  
      callEndpoint @&quot;pay&quot; h1 $ PayParams
          { ppRecipient = pubKeyHash $ walletPubKey $ Wallet 2
          , ppLovelace  = y
          }
      void $ Emulator.waitNSlots 1
      ys &lt;- observableState h1
      Extras.logInfo $ show ys
  
  I retracted the surrounding code that has not been modified.
  The start code can be found here.
upvoted_answer: The exception handler does not cover the first payment contract run. I've created a separate function for this that handles this error:
  payContract' :: Contract () PaySchema Void ()
  payContract' = Contract.handleError
      (\err -&gt; Contract.logError $ &quot;Caught error: &quot; ++ unpack err)
      payContract
  
  Then of course you have to call the payContract' in your emulator tracer as follows:
  h1 &lt;- activateContractWallet (Wallet 1) payContract'
  
  You can also combine everything together like this:
  payContract :: Contract () PaySchema Text ()
  payContract = Contract.handleError
      (\err -&gt; Contract.logError $ &quot;Caught error: &quot; ++ unpack err) $ do
      pp &lt;- endpoint @&quot;pay&quot;
      let tx = mustPayToPubKey (ppRecipient pp) $ lovelaceValueOf $ ppLovelace pp
      void $ submitTx tx
      payContract