mirror of https://github.com/vitalif/phantomjs
Exit on start script error
parent
56039cae7f
commit
7a9e0c8599
|
@ -19,7 +19,7 @@
|
|||
|
||||
import sys
|
||||
|
||||
from PyQt4.QtCore import QObject, QFile, qWarning
|
||||
from PyQt4.QtCore import QObject, QFile
|
||||
from PyQt4.QtWebKit import QWebPage
|
||||
|
||||
|
||||
|
@ -45,7 +45,4 @@ class CSConverter(QObject):
|
|||
[false, error.message];
|
||||
}
|
||||
''')
|
||||
if result[0] is False:
|
||||
qWarning(result[1])
|
||||
return ''
|
||||
return result[1]
|
||||
return result
|
||||
|
|
|
@ -118,12 +118,22 @@ def injectJsInFrame(filePath, libraryPath, targetFrame, startingScript=False):
|
|||
if script.startswith('#!') and not filePath.lower().endswith('.coffee'):
|
||||
script = '//' + script
|
||||
|
||||
script = script if not filePath.lower().endswith('.coffee') else coffee2js(script)
|
||||
if filePath.lower().endswith('.coffee'):
|
||||
result = coffee2js(script)
|
||||
if result[0] is False:
|
||||
if startingScript:
|
||||
sys.exit(result[1])
|
||||
else:
|
||||
qWarning(result[1])
|
||||
script = ''
|
||||
else:
|
||||
script = result[1]
|
||||
|
||||
# prepare start script for exiting
|
||||
if startingScript:
|
||||
script = '''try { %s } catch (err) {
|
||||
if (err !== 'phantom.exit') {
|
||||
phantom._exit(1);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -49,7 +49,7 @@ CSConverter::CSConverter(QObject *parent)
|
|||
m_webPage.mainFrame()->addToJavaScriptWindowObject("converter", this);
|
||||
}
|
||||
|
||||
QString CSConverter::convert(const QString &script)
|
||||
QVariant CSConverter::convert(const QString &script)
|
||||
{
|
||||
setProperty("source", script);
|
||||
QVariant result = m_webPage.mainFrame()->evaluateJavaScript("try {" \
|
||||
|
@ -57,9 +57,5 @@ QString CSConverter::convert(const QString &script)
|
|||
"} catch (error) {" \
|
||||
" [false, error.message];" \
|
||||
"}");
|
||||
if (result.toStringList().at(0) == "false") {
|
||||
qWarning(qPrintable(result.toStringList().at(1)));
|
||||
return QString();
|
||||
}
|
||||
return result.toStringList().at(1);
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ class CSConverter: public QObject
|
|||
{
|
||||
public:
|
||||
CSConverter(QObject *parent = 0);
|
||||
QString convert(const QString &script);
|
||||
QVariant convert(const QString &script);
|
||||
|
||||
private:
|
||||
QWebPage m_webPage;
|
||||
|
|
|
@ -69,7 +69,7 @@ void Utils::messageHandler(QtMsgType type, const char *msg)
|
|||
}
|
||||
}
|
||||
|
||||
QString Utils::coffee2js(const QString &script)
|
||||
QVariant Utils::coffee2js(const QString &script)
|
||||
{
|
||||
// We need only one instance of the CSConverter to survive for the whole life of PhantomJS
|
||||
static CSConverter *coffeeScriptConverter = NULL;
|
||||
|
@ -100,9 +100,20 @@ bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &libraryPat
|
|||
scriptBody.prepend("//");
|
||||
}
|
||||
|
||||
scriptBody = jsFile.fileName().endsWith(COFFEE_SCRIPT_EXTENSION) ?
|
||||
Utils::coffee2js(scriptBody) : //< convert from Coffee Script
|
||||
scriptBody;
|
||||
if (jsFile.fileName().endsWith(COFFEE_SCRIPT_EXTENSION)) {
|
||||
QVariant result = Utils::coffee2js(scriptBody);
|
||||
if (result.toStringList().at(0) == "false") {
|
||||
if (startingScript) {
|
||||
std::cerr << qPrintable(result.toStringList().at(1)) << std::endl;
|
||||
exit(1);
|
||||
} else {
|
||||
qWarning() << qPrintable(result.toStringList().at(1));
|
||||
scriptBody = QString();
|
||||
}
|
||||
} else {
|
||||
scriptBody = result.toStringList().at(1);
|
||||
}
|
||||
}
|
||||
|
||||
// prepare start script for exiting
|
||||
if (startingScript) {
|
||||
|
@ -110,6 +121,7 @@ bool Utils::injectJsInFrame(const QString &jsFilePath, const QString &libraryPat
|
|||
" %1" \
|
||||
"} catch (err) {" \
|
||||
" if (err !== 'phantom.exit') {" \
|
||||
" phantom._exit(1);" \
|
||||
" throw err;" \
|
||||
" }" \
|
||||
"}").arg(scriptBody);
|
||||
|
|
|
@ -45,7 +45,7 @@ class Utils
|
|||
public:
|
||||
static void showUsage();
|
||||
static void messageHandler(QtMsgType type, const char *msg);
|
||||
static QString coffee2js(const QString &script);
|
||||
static QVariant coffee2js(const QString &script);
|
||||
static bool injectJsInFrame(const QString &jsFilePath, const QString &libraryPath, QWebFrame *targetFrame, const bool startingScript = false);
|
||||
|
||||
private:
|
||||
|
|
Loading…
Reference in New Issue