Error handling/slowing down script automation

I’m trying to finish building out our customer wait list with SMS notification. ClickSend allows us to send a POST request for the message and a GET request for the results. Issue is that sometimes they don’t get the results to you really quick and then there is an invocation error and no result since it can’t find the message id in the API.

I found this sleep timer on the forum, but my question is the a better/easier way to handle the error processing, maybe something like a while/for function with the check process along with a timer. I don’t mess with this too often, so if anyone has advice let me know.


***This is a valid response. Its sent to a bad number intentionally.

Every now and then I get

image

Just because the report isn’t there yet.

The PostJson() method is running synchronously as UploadString() is not awaited. You shouldn’t need to use any sleep/delay functions. PostJson() will return control to the calling method when A) a response is received B) timeout is reached or C) an exception is thrown.

Without knowing what the exception is it’s hard to tell what’s going on.

If you catch the exception you can use file.WriteToFile(fileName, content) to log it to a file.

EDIT: you’ll want to call ToString() on the exception to write it to a file.

Thanks Memo. I’ll try to capture that later tonight. I was receiving this error when calling the SMS receipt and the report was available yet (or so I believe).

I think even if I’m able to correct the exception, I’ll need to do something with while/try/catch and max attempts since the SMS report generation on ClickSend’s side isn’t immediate.

So the issue is that the API server returns a 404 when it can’t find the record. This is happening because its still sending the SMS (its not instant) and it doesn’t create the receipt/status record until this is complete.

I’m trying to find a good solution to retry the call with a sleep function. Feel free to provide any ideas you have. Its hard to find references on JScript.

Microsoft.ClearScript.ScriptEngineException: Exception has been thrown by the target of an invocation. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.WebException: The remote server returned an error: (404) Not Found.
   at System.Net.WebClient.DownloadDataInternal(Uri address, WebRequest& request)
   at System.Net.WebClient.DownloadString(Uri address)
   at System.Net.WebClient.DownloadString(String address)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.ClearScript.Util.InvokeHelpers.InvokeMethod(IHostInvokeContext context, Object target, MethodInfo method, Object[] args)
   at Microsoft.ClearScript.HostItem.MethodBindSuccess.Invoke(HostItem hostItem)
   at Microsoft.ClearScript.HostItem.InvokeMethod(String name, Type[] typeArgs, Object[] args, Object[] bindArgs)
   at Microsoft.ClearScript.HostItem.InvokeMethod(String name, Object[] args, Object[] bindArgs)
   at Microsoft.ClearScript.HostItem.InvokeHostMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean& isCacheable)
   at Microsoft.ClearScript.HostItem.InvokeMember(String name, BindingFlags invokeFlags, Object[] args, Object[] bindArgs, CultureInfo culture, Boolean bypassTunneling, Boolean& isCacheable)
   at Microsoft.ClearScript.HostItem.<>c__DisplayClass156_0.<InvokeReflectMember>b__0()
   at Microsoft.ClearScript.ScriptEngine.HostInvoke[T](Func`1 func)
   at Microsoft.ClearScript.Windows.WindowsScriptEngine.HostInvoke[T](Func`1 func)
   --- End of inner exception stack trace ---
   at Microsoft.ClearScript.Windows.WindowsScriptItem.TryBindAndInvoke(DynamicMetaObjectBinder binder, Object[] args, Object& result)
   at Microsoft.ClearScript.ScriptItem.<>c__DisplayClass7_0.<TryWrappedBindAndInvoke>b__0()
   at Microsoft.ClearScript.Windows.WindowsScriptEngine.<>c__DisplayClass57_0`1.<ScriptInvoke>b__0()
   at Microsoft.ClearScript.ScriptEngine.ScriptInvokeInternal[T](Func`1 func)
   at Microsoft.ClearScript.ScriptEngine.ScriptInvoke[T](Func`1 func)
   at Microsoft.ClearScript.Windows.WindowsScriptEngine.ScriptInvoke[T](Func`1 func)
   at Microsoft.ClearScript.ScriptItem.TryWrappedBindAndInvoke(DynamicMetaObjectBinder binder, Object[] wrappedArgs, Object& result)
   at Microsoft.ClearScript.ScriptItem.TryWrappedInvokeOrInvokeMember(DynamicMetaObjectBinder binder, ParameterInfo[] parameters, Object[] args, Object& result)
   at Microsoft.ClearScript.ScriptItem.TryInvoke(InvokeBinder binder, Object[] args, Object& result)
   at CallSite.Target(Closure , CallSite , Object )
   at Microsoft.ClearScript.HostFunctions.tryCatch(Object tryFunc, Object catchFunc, Object finallyFunc)

Use execute action command action and delay field to set a ‘sleep time’ , using a for loop in a script will freeze samba.

Save the response in a program setting and with another rule create a loop to look if the response exist. This is what I’ve done as a ‘hack’ because there is no a sleep function available in the script engine. You should play with field execute in background, i don’t remember which option consumes ram.

You should also create a way to stop the loop…

1 Like

Seeing as there’s no way to pass a cancellation token into ClearScript, I was thinking a do/while but there’s nothing really to await so it would just fire calls to the API which is useless and could put one over the rate limit. hostException.GetBaseException().ToString() will give you just the exception to parse but it’s still not just System.Net.WebException: The remote server returned an error: (xyz) ResponseNameHere. which would be easier to parse.

I like Francisco’s idea. After a successful response from sending the message, execute an automation command in the background with a delay of x amount of seconds to then query for the status. I don’t know if the delay when Background = True happens before the command is the run in the background or if everything is executed in the background including the delay.

Originally I was displaying the status as a pop up but if I do it this way I think it makes more sense to have a sms status indication on the task list. Then I’ll have it hop through stages: no text,pending,confirmed,error. This should be easier than trying to process the check at the same time as request. It will also be a reminder to not send another one.

1 Like

Just a thought but is updating status you could do a sweep to check non sent/error messages for update on say screen change, logout or a cron maybe.
I sync my sales data to hotel PMS on 15 min cron running on server like that.

Getting super close. Just need to handle the different responses. Thanks all for the help, I’ll post it up when I’m done.

1 Like

Thanks for your help guys. I think I have our Wait List widget figured out for the most part. It worked best if I split up submission and verification. Its working pretty seemless now. I just wish I could have duration show within the Content Template.

If thats possible let me know. It seems like I can only put it on the top. Ideally I’d like to put it above my indicator icon.

  1. Upon entry the task is in “waiting status”
  2. Notify calls ClickSend API script and changes status to “notifying”
  3. Upon screen refresh it runs ClickSend API to check for success of SMS

All changes update icon indicator and other info, such as error code, timestamp, etc…

3 Likes