SSJS try and catch and Ampscript raise error

ampscriptmarketing-cloudssjs

At the moment our emails are using a try and catch SSJS wrapping and stringify to log the error into a DE.
We need to be able to catch any error happening due to the use of different blocks. At the moment we use SSJS try and catch, the following error has been logged into the debug DE however it has also been emailed to the customer .

Call to update the salesforceobject Account ID = 0017Y00001XXXXXXX failed! Error status code: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY\nError message: AccountTrigger: execution of AfterUpdate\n\ncaused by: System.DmlException: Update failed. First exception on row 0 with id 0PK7YXXXXX015YtXXXX; first error: UNABLE_TO_LOCK_ROW, unable to obtain exclusive access to this record: []\n\nClass.IndividualService.syncIndividualFields: line 77, column 1\nClass.AccountTriggerHandler.handleAfterUpdate: line 139, column 1\nTrigger.AccountTrigger: line 37, column 1"

We don't want the customer to receive the email with the error message. And we need to block the send when this happens.

Support suggested to use Raise Error to block the send completely as SSJS will log but still send.
I only find example with IF statement, but I don't have a way to see if the record is locked. So I thought to use the SSJS to catch the error and then raise the error with ampscript.

I tried to add the raise error in the SSJS catch part. The email is not sent but also the error is not logged into the data extension.
I am not sure if the following syntax incorrect?
Should the raise error be outside of the SSJS?

As I cannot reproduce the record lock issue I tried by adding the wrong contact key however seems like the try and catch is not catching this error and not logging. So I don't understand if it's a limitation or a syntax issue

     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
          
      <head>
      </head>
      <body>
        
        **<script runat="server">
          Platform.Load("Core","1");
          try{
        </script>**

<div style="font-size:0; line-height:0;">
         
              ***..... email blocks and content......***
                                           
                                      
                                    
        <script runat="server">
          }
          catch(e) {
            Write(Stringify(e.message))
            Platform.Function.InsertDE("EmailDebugLog" ,["Template","ErrorMessage"],["1",Stringify(e.message)]);
            RaiseError("Error occurred",true)
          }
        </script>
    
      </body>
    </html>

Best Answer

Probably not what you want to hear, but you should not rely on a real-time call to a SF object to retrieve a coupon in your email scripting.

If you want a performant and stable process, I'd recommend storing your coupons in a Data Extension and doing a simple lookup or claimrow to retrieve them at send time.

You'll forever be battling locking and latency issues if you continue down this path.

Since your account is integrated, why not just refresh a coupon DE of all SF Contacts populated with an hourly SQL query?