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 )
|
if ( !js_result )
|
||||||
{
|
{
|
||||||
wxLogWarning(_("Error running JavaScript: %s"), error.GetMessage());
|
if ( output )
|
||||||
|
*output = error.GetMessage();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1158,8 +1159,11 @@ bool wxWebViewWebKit::RunScriptSync(const wxString& javascript, wxString* output
|
|||||||
|
|
||||||
if ( exception )
|
if ( exception )
|
||||||
{
|
{
|
||||||
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
|
if ( output )
|
||||||
wxLogWarning(_("Exception running JavaScript: %s"), ex_value.ToWxString());
|
{
|
||||||
|
wxJSStringRef ex_value(JSValueToStringCopy(context, exception, NULL));
|
||||||
|
*output = ex_value.ToWxString();
|
||||||
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -1172,23 +1176,31 @@ 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) )
|
||||||
|
{
|
||||||
|
if ( output )
|
||||||
|
*output = result;
|
||||||
|
result.clear();
|
||||||
|
}
|
||||||
|
|
||||||
RunScriptSync(wrapJS.GetCleanUpCode());
|
RunScriptSync(wrapJS.GetCleanUpCode());
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wxLogWarning(_("JavaScript error: %s"), result);
|
if ( !result.empty() )
|
||||||
return false;
|
{
|
||||||
|
wxLogWarning(_("Error running JavaScript: %s"), result);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
void wxWebViewWebKit::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
||||||
|
@@ -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,24 +909,35 @@ 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 )
|
||||||
*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) )
|
CallEval(wrapJS.GetCleanUpCode(), scriptAO, &varResult);
|
||||||
return false;
|
}
|
||||||
return true;
|
else // result available but not the expected "true"
|
||||||
|
{
|
||||||
|
err = varResult.MakeString();
|
||||||
}
|
}
|
||||||
|
|
||||||
wxLogWarning(_("JavaScript error: %s"), varResult.MakeString());
|
if ( !err.empty() )
|
||||||
return false;
|
{
|
||||||
|
wxLogWarning(_("Error running JavaScript: %s"), varResult.MakeString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
void wxWebViewIE::RegisterHandler(wxSharedPtr<wxWebViewHandler> handler)
|
||||||
|
@@ -417,26 +417,42 @@ bool wxWebViewWebKit::RunScript(const wxString& javascript, wxString* output)
|
|||||||
wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount);
|
wxJSScriptWrapper wrapJS(javascript, &m_runScriptCount);
|
||||||
|
|
||||||
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);
|
{
|
||||||
|
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 )
|
err = wxCFStringRef::AsString(result);
|
||||||
wxLogWarning(_("JavaScript error: %s"), wxCFStringRef::AsString(result));
|
}
|
||||||
|
|
||||||
|
if ( !err.empty() )
|
||||||
|
{
|
||||||
|
wxLogWarning(_("Error running JavaScript: %s"), err);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user