Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Exit on start script error
Browse files Browse the repository at this point in the history
  • Loading branch information
IceArmy committed Jun 19, 2011
1 parent 56039ca commit 7a9e0c8
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
7 changes: 2 additions & 5 deletions python/pyphantomjs/csconverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import sys

from PyQt4.QtCore import QObject, QFile, qWarning
from PyQt4.QtCore import QObject, QFile
from PyQt4.QtWebKit import QWebPage


Expand All @@ -45,7 +45,4 @@ def convert(self, script):
[false, error.message];
}
''')
if result[0] is False:
qWarning(result[1])
return ''
return result[1]
return result
12 changes: 11 additions & 1 deletion python/pyphantomjs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
8 changes: 2 additions & 6 deletions src/csconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,13 @@ 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 {" \
" [true, this.CoffeeScript.compile(converter.source)];" \
"} 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;
}
2 changes: 1 addition & 1 deletion src/csconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,16 +100,28 @@ 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) {
scriptBody = QString("try {" \
" %1" \
"} catch (err) {" \
" if (err !== 'phantom.exit') {" \
" phantom._exit(1);" \
" throw err;" \
" }" \
"}").arg(scriptBody);
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7a9e0c8

Please sign in to comment.