Hi Jose Luis,
I would use more than one error-handling subroutine. If connecting to the email provider fails, trying to send an email on the error-handling subroutine will also fail; and when a command fails in the error-handling subroutine, you can't recover. Thus, you need to separate concerns.
The following snippet shows an example; you can copy the code, paste the code on your RPA Studio and study the example:
defVar --name emailPasword --type SecureString
defVar --name imapConnection --type EmailConnection
defVar --name emails --type List --innertype String
defVar --name errorMessage --type String
// This script does not work: it is intended as an example of how to deal with multiple error contexts.
// You can use onError multiple times. onError is a scope-based command: it wraps the commands of the subroutine, and inner subroutines, he is in.
// Imagine your script does a bunch of stuff. Eventually, the script tries to send an email.
onError --label handleGeneralError --comment "This Handle Error deals with any errors in a generic way."
goSub --label SendEmail
beginSub --name handleGeneralError
// Handle any errors in a generic way: log and try to send email.
setVar --name "${errorMessage}" --value "An error occured:\r\n\r\n${wdg:error}"
logMessage --message "${errorMessage}" --type "Error"
imapConnect --mailhost "example.imap.com" --mailport 993 --usessl --UseConnectionToSend --smtpcredentials --smtphost "example.smtp.com" --smtpport 443 --smtpusername "email@example.com" --smtppassword "${emailPasword}" --smtpusessl --mailusername "email@example.com" --mailpassword "${emailPasword}" imapConnection=value
emailSend --connection ${imapConnection} --to ${emails} --from "email@example.com" --bodytype "Text" --body "${errorMessage}"
emailDisconnect --connection ${imapConnection}
endSub
beginSub --name SendEmail
// Now, this is a crucial part of the process. If this task fails, trying to send the email again will be redundant: it already failed. So you can use a new onError here to redirect to another error-handling approach.
onError --label handleErrorSendingEmail
imapConnect --mailhost "example.imap.com" --mailport 993 --usessl --UseConnectionToSend --smtpcredentials --smtphost "example.smtp.com" --smtpport 443 --smtpusername "email@example.com" --smtppassword "${emailPasword}" --smtpusessl --mailusername "email@example.com" --mailpassword "${emailPasword}" imapConnection=value
emailSend --connection ${imapConnection} --to ${emails} --from "email@example.com" --bodytype "Text" --body "\"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\""
emailDisconnect --connection ${imapConnection}
endSub
beginSub --name handleErrorSendingEmail
// Handle error related to sending emails. In here, trying to send the email again is redundant: it already failed. So what you could do is use a different error-handling approach. In this example, the script logs the error in the local log file, in the cloud, and also as a custom log file.
setVar --name "${errorMessage}" --value "An error occured:\r\n\r\n${wdg:error}"
logMessage --message "${errorMessage}" --type "Error"
writeToFile --value " ${errorMessage}" --file "a/folder/where/you/store/logs.txt" --encoding "Default"
endSub
------------------------------
Gabriel Sanchez
------------------------------