cancel
Showing results for 
Search instead for 
Did you mean: 

Problem of sending email within try block

gordon_liang
Champ on-the-rise
Champ on-the-rise
I wrote a javascript to invoke after a document is created in a folder (through the rule). In this script, I tried to send an email.
I put "mail.execute(document)" in the try block, so that it can catch the exception when sending an email (like connection issue to SMTP server, or email address error etc).

After an error happens when sending the mail, the catch statements get hit as expected. However looks like the script still returns error so that I got the error message of "Exception from executeScript - redirecting to status template error: Transaction silently rolled back because it has been marked as rollback-only".  The document is still created after this error message though.

On the other hand, if I inject some other exception (such as null object access) into the try block, the exception is catched and no error message returned when creating a document.

What's the problem in catching the excepting when sending the mail?


The javascript code is like below (I removed some unrelated codes)


                 var mail = actions.create("mail");

                 mail.parameters.html = "html";

                 mail.parameters.to = "b@example.com";

                 mail.parameters.template = search.findNode("workspace://SpacesStore/87d845a1-1639-4de8-b14a-b7c0add7d559");
                 mail.parameters.subject = "Title";
                 mail.parameters.from = "a@example.com";

                 try
                 {
                   mail.execute(document);  // if comment this line but un-comment next two lines, the exception can be catch
                                            // and every thing fine after the script completes.
                   //var a=null;
                   //a.set("abc");
                    logger.warn ("Email sent.");
                 } catch (err)
                 {
                     logger.warn ("Mail sent failed. Errormsg: " + err.message);
                 }
3 REPLIES 3

rjohnson
Star Contributor
Star Contributor
I have never managed to use try / catch is a JavaScripr rule effectively. it does some very odd things. In my experience the error is always reported despite the try / catch and the transaction rolled back.

darkredd
Star Contributor
Star Contributor
Hi,

I have not tried try/catch on a rule before, so I can't comment on that. However, if all you want is to propagate an error when there is a problem with executing the mail; try the following:
mail.parameters.ignore_send_failure = false;
Essentially, this says if the mail actions runs into a problem roll back and report on the logs.
It has always worked for me, though I have only done it outside of a rule.

Thanks for the comments. I just realized the rule of exception handling within the javascript of Alfresco. The try/catch block looks like runnig well in the script, however the error is not ignored by the Alfresco rule. Looks like this is a design intention of the Alfresco rules, though it would be weird to me.

As for the ignore_send_failure parameter… Actually what I want is NOT to propagate an error when sending an email. Because I would like the creation of document be success anyway, no matter whether a mail is sent properly or not. Furthermore, the mail would be sent to a couple of users. If the mail address of one user is not right, it doesn't make sense to stop sending to the other users… In this case, shall I set it to ignore_send_failure = true?

BTW, where to find the official documents of the send mail action? In the doc of Alfresco, I can only find the high level documentation of actions but not the specific ones.