Skip to content

Commit

Permalink
Merge pull request #212 from pec1985/timob-14365
Browse files Browse the repository at this point in the history
[TIMOB-14365] don't crash the app if JS error
  • Loading branch information
pec1985 committed Apr 4, 2014
2 parents 192c223 + a489bdd commit a2e1258
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 216 deletions.
121 changes: 9 additions & 112 deletions src/tibb/TiRootObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,44 +56,6 @@ using namespace titanium;
static Handle<ObjectTemplate> g_rootTemplate;

static QMap<QString, v8::Persistent<Value> > _commonJSModules;
static QString ThrowJSException(TryCatch tryCatch)
{
QString str;
HandleScope scope;

if(tryCatch.HasCaught())
{
Handle<Message> message = tryCatch.Message();
if(!message.IsEmpty()) {
Handle<Value> fileName = message->GetScriptResourceName();
Local<String> srcLine = message->GetSourceLine();
Ti::TiHelper::Log(QString("File Name: ").append(Ti::TiHelper::QStringFromValue(fileName)));
Ti::TiHelper::Log(QString("Source Line: ").append(Ti::TiHelper::QStringFromValue(srcLine)));
str.append("File: ").append(Ti::TiHelper::QStringFromValue(fileName)).append("\n");
str.append("Source: ").append(Ti::TiHelper::QStringFromValue(srcLine)).append("\n\n");
}
QString stackTrace = Ti::TiHelper::QStringFromValue(tryCatch.StackTrace());
if (!stackTrace.isEmpty()) {
str.append(stackTrace);
} else {
Local<Value> er = tryCatch.Exception();

bool isErrorObject =
er->IsObject() &&
!(er->ToObject()->Get(String::New("message"))->IsUndefined()) &&
!(er->ToObject()->Get(String::New("name"))->IsUndefined());

if (isErrorObject) {
Local<String> name = er->ToObject()->Get(String::New("name"))->ToString();
str.append(Ti::TiHelper::QStringFromValue(name) + "\n");
}
Local<String> message = !isErrorObject ? er->ToString() : er->ToObject()->Get(String::New("message"))->ToString();
str.append(Ti::TiHelper::QStringFromValue(message) + "\n");
}

}
return str;
}


TiRootObject::TiRootObject()
Expand Down Expand Up @@ -210,94 +172,29 @@ int TiRootObject::executeScript(NativeObjectFactory* objectFactory, const char*
Handle<Script> compiledBootstrapScript = Script::Compile(String::New(bootstrapJavascript.c_str()), String::New(bootstrapFilename));
if (compiledBootstrapScript.IsEmpty())
{
String::Utf8Value error(tryCatch.Exception());
TiLogger::getInstance().log(*error);
return -1;
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return (messageLoopEntry)(context);
}
Handle<Value> bootstrapResult = compiledBootstrapScript->Run();
if (bootstrapResult.IsEmpty())
{
Local<Value> exception = tryCatch.Exception();
// FIXME: need a way to prevent double "filename + line" output
Handle<Message> msg = tryCatch.Message();
stringstream ss;
ss << bootstrapFilename << " line ";
if (msg.IsEmpty())
{
ss << "?";
}
else
{
ss << msg->GetLineNumber();
}
ss << ": " << *String::Utf8Value(exception);
TiLogger::getInstance().log(ss.str().c_str());
return -1;
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return (messageLoopEntry)(context);
}

const char* filename = "app.js";
Handle<Script> compiledScript = Script::Compile(String::New(javaScript), String::New(filename));
QString error_str;
if (compiledScript.IsEmpty())
{
ThrowJSException(tryCatch);
return 1;
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return (messageLoopEntry)(context);
}
compiledScript->Run();
if (tryCatch.HasCaught())
{
error_str = ThrowJSException(tryCatch);
scriptHasError = true;
}
// show script error
QString deployType = Ti::TiHelper::getAppSetting("deploytype").toString();
if (scriptHasError && deployType.compare(QString("development")) == 0) {

bb::cascades::Page* page = bb::cascades::Page::create();
bb::cascades::Container* background = bb::cascades::Container::create();
background->setLayout(bb::cascades::DockLayout::create());
background->setBackground(bb::cascades::Color::Red);
page->setContent(background);

bb::cascades::Container* container = bb::cascades::Container::create();
container->setHorizontalAlignment(bb::cascades::HorizontalAlignment::Center);
container->setVerticalAlignment(bb::cascades::VerticalAlignment::Center);
background->add(container);

bb::cascades::Label *label = bb::cascades::Label::create();
label->textStyle()->setColor(bb::cascades::Color::White);
label->setText(error_str);
label->setMultiline(true);
container->add(label);

bb::cascades::Application::instance()->setScene(bb::cascades::Page::create());

bb::cascades::Sheet *sheet = bb::cascades::Sheet::create();
sheet->setPeekEnabled(false);
sheet->setContent(page);
sheet->open();


/*
static const string javaScriptErrorAlert = string("var win1 = Titanium.UI.createWindow({") +
string("backgroundColor:'red'") +
string("});") +
string("var label1 = Titanium.UI.createLabel({") +
string("color:'white',") +
string("textAlign:'center',") +
string("text:") +
"'" + err_msg + "'," +
string("font:{fontSize:8,fontFamily:'Helvetica Neue',fontStyle:'Bold'},") +
string("});") +
string("win1.add(label1);") +
string("win1.open();");
compiledScript = Script::Compile(String::New(javaScriptErrorAlert.c_str()));
compiledScript->Run();
*/
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
}

onStartMessagePump();
return (messageLoopEntry)(context);
}
Expand Down Expand Up @@ -412,13 +309,13 @@ Handle<Value> TiRootObject::_require(void* userContext, TiObject* caller, const
TryCatch tryCatch;
if (script.IsEmpty())
{
ThrowException(tryCatch.ReThrow());
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return scope.Close(Undefined());
}
Persistent<Value> result = Persistent<Value>::New(script->Run());
if (result.IsEmpty())
{
ThrowException(tryCatch.ReThrow());
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return scope.Close(Undefined());
}
_commonJSModules.insert(filePath, result);
Expand Down
12 changes: 5 additions & 7 deletions src/tibb/TiTitaniumObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "TiAnalyticsObject.h"
#include "TiV8EventContainerFactory.h"
#include "Contacts/ContactsModule.h"

#include "TiCore.h"
#include "V8Utils.h"

#include <fstream>
Expand Down Expand Up @@ -166,17 +166,15 @@ static Handle<Value> includeJavaScript(string id, string parentFolder, bool* err
Handle<Script> compiledScript = Script::Compile(String::New(javascript.c_str()), String::New(filename.c_str()));
if (compiledScript.IsEmpty())
{
*error = true;
std::string err_msg;
DisplayExceptionLine(tryCatch, err_msg);
return tryCatch.ReThrow();
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return Undefined();
}

Local<Value> result = compiledScript->Run();
if (result.IsEmpty())
{
*error = true;
return tryCatch.ReThrow();
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
return Undefined();
}

return result;
Expand Down
42 changes: 1 addition & 41 deletions src/tibb/TiV8Event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,6 @@
#include "TiAPIObject.h"
#include "TiCore.h"

static QString ThrowJSException(TryCatch tryCatch)
{
QString str;
HandleScope scope;

if(tryCatch.HasCaught())
{
Handle<Message> message = tryCatch.Message();
if(!message.IsEmpty()) {
Handle<Value> fileName = message->GetScriptResourceName();
Local<String> srcLine = message->GetSourceLine();
str.append("File: ").append(Ti::TiHelper::QStringFromValue(fileName)).append("\n");
str.append("Source: ").append(Ti::TiHelper::QStringFromValue(srcLine)).append("\n\n");
}
QString stackTrace = Ti::TiHelper::QStringFromValue(tryCatch.StackTrace());
if (!stackTrace.isEmpty()) {
str.append(stackTrace);
} else {
Local<Value> er = tryCatch.Exception();

bool isErrorObject =
er->IsObject() &&
!(er->ToObject()->Get(String::New("message"))->IsUndefined()) &&
!(er->ToObject()->Get(String::New("name"))->IsUndefined());

if (isErrorObject) {
Local<String> name = er->ToObject()->Get(String::New("name"))->ToString();
str.append(Ti::TiHelper::QStringFromValue(name) + "\n");
}
Local<String> message = !isErrorObject ? er->ToString() : er->ToObject()->Get(String::New("message"))->ToString();
str.append(Ti::TiHelper::QStringFromValue(message) + "\n");
}

}
return str;
}

TiV8Event::TiV8Event()
{
}
Expand Down Expand Up @@ -89,10 +52,7 @@ void TiV8Event::fire(void* fireDataObject)
}
if (result.IsEmpty())
{
tryCatch.SetVerbose(true);
String::Utf8Value error(tryCatch.Exception());
TiAPIObject::Log(QString("[ERROR] ").append(*error));
TiAPIObject::Log(QString("[ERROR] ").append(ThrowJSException(tryCatch)));
Ti::TiErrorScreen::ShowWithTryCatch(tryCatch);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ticore/include/TiCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
#include "Ti_ViewProxy.h"
#include "Ti_SceneManager.h"
#include "TitaniumLayout.h"

#include "Ti_ErrorScreen.h"
#endif
42 changes: 42 additions & 0 deletions src/ticore/include/Ti_ErrorScreen.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2014 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/

#ifndef TI_ERROR_SCREEN_H_
#define TI_ERROR_SCREEN_H_

#include <v8.h>
#include "TiCore.h"
#include <QObject>
#include <bb/cascades/Sheet>

using namespace v8;
namespace Ti {

class TiErrorScreen : public QObject
{
Q_OBJECT;
public:
static void ShowWithTryCatch(TryCatch);
private:
TiErrorScreen();
~TiErrorScreen();
void parseTryCatch(TryCatch);
void buildAndShow();
QString _fileName;
QString _sourceLine;
QString _stackTrace;
QString _exceptionName;
QString _exceptionMessage;
int _lineNumber;
int _colNumber;
bb::cascades::Sheet *_errorSheet;
public slots:
void onClicked();
void onClosed();
};
}
#endif
1 change: 1 addition & 0 deletions src/ticore/include/Ti_Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class TiHelper {
TiHelper();
virtual ~TiHelper();
static void Log(QString);
static void Log(QString, QString);
static float PPI();
static Handle<Value> Log(Handle<Value>);
static Handle<Value> Log(const Arguments &args);
Expand Down

0 comments on commit a2e1258

Please sign in to comment.