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 ) if ( !js_result )
{ {
wxLogWarning(_("Error running JavaScript: %s"), error.GetMessage()); if ( output )
*output = error.GetMessage();
return false; return false;
} }
@@ -1157,9 +1158,12 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
); );
if ( exception ) if ( exception )
{
if ( output )
{ {
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL)); wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
wxLogWarning(_("Exception running JavaScript: %s"), ex_value.ToWxString()); *output = ex_value.ToWxString();
}
return false; return false;
} }
@@ -1172,25 +1176,33 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
bool wxWebViewWebKit::RunScript(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); 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; wxString result;
bool isValidJS = RunScriptSync(wrapJS.GetWrappedCode(), &result); if ( RunScriptSync(wrapJS.GetWrappedCode(), &result)
&& result == wxS("true") )
if ( isValidJS && result == "true" )
{ {
RunScriptSync(wrapJS.GetOutputCode(), output); if ( RunScriptSync(wrapJS.GetOutputCode(), &result) )
RunScriptSync(wrapJS.GetCleanUpCode()); {
return true; 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 false;
} }
return true;
}
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
{ {
m_handlerList.push_back(handler); m_handlerList.push_back(handler);

View File

@@ -885,13 +885,7 @@ bool CallEval(const wxString& code,
wxVariant* varResult) wxVariant* varResult)
{ {
wxVariant varCode(code); wxVariant varCode(code);
if ( !scriptAO.Invoke("eval", DISPATCH_METHOD, *varResult, 1, &varCode) ) return scriptAO.Invoke("eval", DISPATCH_METHOD, *varResult, 1, &varCode);
{
wxLogWarning(_("Can't run JavaScript"));
return false;
}
return true;
} }
bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output) bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output)
@@ -906,7 +900,7 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output)
IDispatch* scriptDispatch = NULL; IDispatch* scriptDispatch = NULL;
if ( FAILED(document->get_Script(&scriptDispatch)) ) if ( FAILED(document->get_Script(&scriptDispatch)) )
{ {
wxLogWarning(_("Can't get the JavaScript")); wxLogWarning(_("Can't get the JavaScript object"));
return false; return false;
} }
@@ -915,26 +909,37 @@ bool wxWebViewIE::RunScript(const wxString& javascript, wxString* output)
wxAutomationObject scriptAO(scriptDispatch); wxAutomationObject scriptAO(scriptDispatch);
wxVariant varResult; wxVariant varResult;
wxString err;
if ( !CallEval(wrapJS.GetWrappedCode(), scriptAO, &varResult) ) if ( !CallEval(wrapJS.GetWrappedCode(), scriptAO, &varResult) )
return false;
if ( varResult.IsType("bool") && varResult.GetBool() )
{ {
if ( !CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) ) err = _("failed to evaluate");
return false; }
else if ( varResult.IsType("bool") && varResult.GetBool() )
{
if ( output != NULL ) if ( output != NULL )
{
if ( CallEval(wrapJS.GetOutputCode(), scriptAO, &varResult) )
*output = varResult.MakeString(); *output = varResult.MakeString();
else
if ( !CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult) ) err = _("failed to retrieve execution result");
return false;
return true;
} }
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 false;
} }
return true;
}
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler) void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
{ {
wxDynamicLibrary urlMon(wxT("urlmon.dll")); wxDynamicLibrary urlMon(wxT("urlmon.dll"));

View File

@@ -419,24 +419,40 @@ bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output)
NSString* result = [m_webView stringByEvaluatingJavaScriptFromString: 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: result = [m_webView stringByEvaluatingJavaScriptFromString:
wxCFStringRef( wrapJS.GetOutputCode() ).AsNSString()]; wxCFStringRef( wrapJS.GetOutputCode() ).AsNSString()];
[m_webView stringByEvaluatingJavaScriptFromString: [m_webView stringByEvaluatingJavaScriptFromString:
wxCFStringRef( wrapJS.GetCleanUpCode() ). wxCFStringRef( wrapJS.GetCleanUpCode() ).AsNSString()];
AsNSString()];
if ( result != nil && output != NULL ) if ( output != NULL )
*output = wxCFStringRef::AsString(result);
}
else
{ {
if ( result != nil ) if ( result )
wxLogWarning(_("JavaScript error: %s"), wxCFStringRef::AsString(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 false;
} }
return true; return true;
} }