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:
Vadim Zeitlin
2017-10-22 00:22:24 +02:00
parent b3bd9c77af
commit 1bb8cd860a
3 changed files with 76 additions and 43 deletions

View File

@@ -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;
}
@@ -1158,8 +1159,11 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
if ( exception )
{
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
wxLogWarning(_("Exception running JavaScript: %s"), ex_value.ToWxString());
if ( output )
{
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
*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);
if ( RunScriptSync(wrapJS.GetOutputCode(), &result) )
{
if ( output )
*output = result;
result.clear();
}
RunScriptSync(wrapJS.GetCleanUpCode());
return true;
}
wxLogWarning(_("JavaScript error: %s"), result);
return false;
if ( !result.empty() )
{
wxLogWarning(_("Error running JavaScript: %s"), result);
return false;
}
return true;
}
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)

View File

@@ -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 )
*output = varResult.MakeString();
{
if ( CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) )
*output = varResult.MakeString();
else
err = _("failed to retrieve execution result");
}
if ( !CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult) )
return false;
return true;
CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult);
}
else // result available but not the expected "true"
{
err = varResult.MakeString();
}
wxLogWarning(_("JavaScript error: %s"), varResult.MakeString());
return false;
if ( !err.empty() )
{
wxLogWarning(_("Error running JavaScript: %s"), varResult.MakeString());
return false;
}
return true;
}
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)

View File

@@ -417,26 +417,42 @@ bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output)
wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount);
NSString* result = [m_webView stringByEvaluatingJavaScriptFromString:
wxCFStringRef( wrapJS.GetWrappedCode() ).AsNSString()];
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()];
wxCFStringRef( wrapJS.GetOutputCode() ).AsNSString()];
[m_webView stringByEvaluatingJavaScriptFromString:
wxCFStringRef( wrapJS.GetCleanUpCode() ).
AsNSString()];
wxCFStringRef( wrapJS.GetCleanUpCode() ).AsNSString()];
if ( result != nil && output != NULL )
*output = wxCFStringRef::AsString(result);
if ( output != NULL )
{
if ( result )
*output = wxCFStringRef::AsString(result);
else
err = _("failed to retrieve execution result");
}
}
else
else // result available but not the expected "true"
{
if ( result != nil )
wxLogWarning(_("JavaScript error: %s"), wxCFStringRef::AsString(result));
err = wxCFStringRef::AsString(result);
}
if ( !err.empty() )
{
wxLogWarning(_("Error running JavaScript: %s"), err);
return false;
}
return true;
}