Reputation: 95
Say for example that I am running a script through cefSharp using the ExecuteJavaScriptAsync(..) method and that an error is thrown while running the script, how can I detect it and then catch it in my c# program ?
Upvotes: 5
Views: 1200
Reputation: 259
If you need to evaluate code which returns a value, use the Task EvaluateScriptAsync(string script, TimeSpan? timeout) method. Javascript code is executed asynchronously and as such uses the .Net Task class to return a response, which contains error message, result and a success (bool) flag.
// Get Document Height
var task = frame.EvaluateScriptAsync("(function() { var body = document.body, html = document.documentElement; return Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight ); })();", null);
task.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
EvaluateJavaScriptResult = response.Success ? (response.Result ?? "null") : response.Message;
}
}, TaskScheduler.FromCurrentSynchronizationContext());
Upvotes: 7