Improve and harmonize error reporting in RunScript()
Errors were reported in different ways (including not being reported at all in wxMSW) in different ports. Try to consistently do it in the same way now and use exactly the same sentences to facilitate translators life.
This commit is contained in:
@@ -1141,7 +1141,8 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
|
||||
|
||||
if ( !js_result )
|
||||
{
|
||||
wxLogWarning(_("Error running JavaScript: %s"), error.GetMessage());
|
||||
if ( output )
|
||||
*output = error.GetMessage();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1157,9 +1158,12 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
|
||||
);
|
||||
|
||||
if ( exception )
|
||||
{
|
||||
if ( output )
|
||||
{
|
||||
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
|
||||
wxLogWarning(_("Exception running JavaScript: %s"), ex_value.ToWxString());
|
||||
*output = ex_value.ToWxString();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -1172,23 +1176,31 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
|
||||
|
||||
bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output)
|
||||
{
|
||||
wxCHECK_MSG( m_web_view, false,
|
||||
wxS("wxWebView must be created before calling RunScript()") );
|
||||
|
||||
wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount);
|
||||
|
||||
// This string is also used as an error indicator: it's cleared if there is
|
||||
// no error or used in the warning message below if there is one.
|
||||
wxString result;
|
||||
bool isValidJS = RunScriptSync(wrapJS.GetWrappedCode(), &result);
|
||||
|
||||
if ( isValidJS && result == "true" )
|
||||
if ( RunScriptSync(wrapJS.GetWrappedCode(), &result)
|
||||
&& result == wxS("true") )
|
||||
{
|
||||
RunScriptSync(wrapJS.GetOutputCode(), output);
|
||||
RunScriptSync(wrapJS.GetCleanUpCode());
|
||||
return true;
|
||||
if ( RunScriptSync(wrapJS.GetOutputCode(), &result) )
|
||||
{
|
||||
if ( output )
|
||||
*output = result;
|
||||
result.clear();
|
||||
}
|
||||
|
||||
wxLogWarning(_("JavaScript error: %s"), result);
|
||||
RunScriptSync(wrapJS.GetCleanUpCode());
|
||||
}
|
||||
|
||||
if ( !result.empty() )
|
||||
{
|
||||
wxLogWarning(_("Error running JavaScript: %s"), result);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
||||
|
@@ -885,13 +885,7 @@ bool CallEval(const wxString& code,
|
||||
wxVariant* varResult)
|
||||
{
|
||||
wxVariant varCode(code);
|
||||
if ( !scriptAO.Invoke("eval", DISPATCH_METHOD, *varResult, 1, &varCode) )
|
||||
{
|
||||
wxLogWarning(_("Can't run JavaScript"));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return scriptAO.Invoke("eval", DISPATCH_METHOD, *varResult, 1, &varCode);
|
||||
}
|
||||
|
||||
bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output)
|
||||
@@ -906,7 +900,7 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output)
|
||||
IDispatch* scriptDispatch = NULL;
|
||||
if ( FAILED(document->get_Script(&scriptDispatch)) )
|
||||
{
|
||||
wxLogWarning(_("Can't get the JavaScript"));
|
||||
wxLogWarning(_("Can't get the JavaScript object"));
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -915,24 +909,35 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output)
|
||||
wxAutomationObject scriptAO(scriptDispatch);
|
||||
wxVariant varResult;
|
||||
|
||||
wxString err;
|
||||
if ( !CallEval(wrapJS.GetWrappedCode(), scriptAO, &varResult) )
|
||||
return false;
|
||||
|
||||
if ( varResult.IsType("bool") && varResult.GetBool() )
|
||||
{
|
||||
if ( !CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) )
|
||||
return false;
|
||||
|
||||
err = _("failed to evaluate");
|
||||
}
|
||||
else if ( varResult.IsType("bool") && varResult.GetBool() )
|
||||
{
|
||||
if ( output != NULL )
|
||||
{
|
||||
if ( CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) )
|
||||
*output = varResult.MakeString();
|
||||
|
||||
if ( !CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult) )
|
||||
return false;
|
||||
return true;
|
||||
else
|
||||
err = _("failed to retrieve execution result");
|
||||
}
|
||||
|
||||
wxLogWarning(_("JavaScript error: %s"), varResult.MakeString());
|
||||
CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult);
|
||||
}
|
||||
else // result available but not the expected "true"
|
||||
{
|
||||
err = varResult.MakeString();
|
||||
}
|
||||
|
||||
if ( !err.empty() )
|
||||
{
|
||||
wxLogWarning(_("Error running JavaScript: %s"), varResult.MakeString());
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
||||
|
@@ -419,24 +419,40 @@ bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output)
|
||||
NSString* result = [m_webView stringByEvaluatingJavaScriptFromString:
|
||||
wxCFStringRef( wrapJS.GetWrappedCode() ).AsNSString()];
|
||||
|
||||
if ( result != nil && [result isEqualToString:@"true"] )
|
||||
wxString err;
|
||||
if ( result == nil )
|
||||
{
|
||||
// This is not very informative, but we just don't have any other
|
||||
// information in this case.
|
||||
err = _("failed to evaluate");
|
||||
}
|
||||
else if ( [result isEqualToString:@"true"] )
|
||||
{
|
||||
result = [m_webView stringByEvaluatingJavaScriptFromString:
|
||||
wxCFStringRef( wrapJS.GetOutputCode() ).AsNSString()];
|
||||
|
||||
[m_webView stringByEvaluatingJavaScriptFromString:
|
||||
wxCFStringRef( wrapJS.GetCleanUpCode() ).
|
||||
AsNSString()];
|
||||
wxCFStringRef( wrapJS.GetCleanUpCode() ).AsNSString()];
|
||||
|
||||
if ( result != nil && output != NULL )
|
||||
*output = wxCFStringRef::AsString(result);
|
||||
}
|
||||
else
|
||||
if ( output != NULL )
|
||||
{
|
||||
if ( result != nil )
|
||||
wxLogWarning(_("JavaScript error: %s"), wxCFStringRef::AsString(result));
|
||||
if ( result )
|
||||
*output = wxCFStringRef::AsString(result);
|
||||
else
|
||||
err = _("failed to retrieve execution result");
|
||||
}
|
||||
}
|
||||
else // result available but not the expected "true"
|
||||
{
|
||||
err = wxCFStringRef::AsString(result);
|
||||
}
|
||||
|
||||
if ( !err.empty() )
|
||||
{
|
||||
wxLogWarning(_("Error running JavaScript: %s"), err);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user