X Tutup
Skip to content

Commit 1ebe5a9

Browse files
author
Justin McPherson
committed
Support RedBox.
Display red error box on errors. - When code fails to load from source (packager or otherwise). - When there is an exception as reported by ExceptionsManager. The use may reload the JS if the error has been fixed.
1 parent 99cac36 commit 1ebe5a9

File tree

8 files changed

+486
-1
lines changed

8 files changed

+486
-1
lines changed

ReactUbuntu/runtime/src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ set(
4747
reacttextproperties.cpp
4848
reactrawtextproperties.cpp
4949
reactuimanager.cpp
50+
reactredboxitem.cpp
51+
reactexceptionsmanager.cpp
5052
ubuntucomponentsloader.cpp
5153
ubuntucomponentmodule.cpp
5254
ubuntuscrollviewmanager.cpp

ReactUbuntu/runtime/src/reactbridge.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
#include "reactimagemanager.h"
4141
#include "reactuimanager.h"
4242
#include "reactimageloader.h"
43+
#include "reactredboxitem.h"
44+
#include "reactexceptionsmanager.h"
4345

4446
#include "ubuntuscrollviewmanager.h"
4547
#include "ubuntunavigatormanager.h"
@@ -56,6 +58,7 @@ class ReactBridgePrivate
5658
ReactExecutor* executor = nullptr;
5759
QQmlEngine* qmlEngine = nullptr;
5860
QQuickItem* visualParent = nullptr;
61+
ReactRedboxItem* redbox = nullptr;
5962
QNetworkAccessManager* nam = nullptr;
6063
ReactUIManager* uiManager = nullptr;
6164
ReactImageLoader* imageLoader = nullptr;
@@ -75,6 +78,7 @@ class ReactBridgePrivate
7578
new ReactRawTextManager,
7679
new ReactTextManager,
7780
new ReactImageManager,
81+
new ReactExceptionsManager,
7882
};
7983
}
8084

@@ -267,6 +271,15 @@ ReactImageLoader* ReactBridge::imageLoader() const
267271
return d_func()->imageLoader;
268272
}
269273

274+
275+
ReactRedboxItem* ReactBridge::redbox()
276+
{
277+
Q_D(ReactBridge);
278+
if (d->redbox == nullptr)
279+
d->redbox = new ReactRedboxItem(this);
280+
return d->redbox;
281+
}
282+
270283
void ReactBridge::sourcesFinished()
271284
{
272285
Q_D(ReactBridge);
@@ -276,6 +289,12 @@ void ReactBridge::sourcesFinished()
276289
});
277290
}
278291

292+
void ReactBridge::sourcesLoadFailed()
293+
{
294+
Q_D(ReactBridge);
295+
redbox()->showErrorMessage("Failed to load source code");
296+
}
297+
279298
void ReactBridge::loadSource()
280299
{
281300
Q_D(ReactBridge);
@@ -306,6 +325,7 @@ void ReactBridge::initModules()
306325
// XXX:
307326
d->sourceCode->setScriptUrl(d->bundleUrl);
308327
connect(d->sourceCode, SIGNAL(sourceCodeChanged()), SLOT(sourcesFinished()));
328+
connect(d->sourceCode, SIGNAL(loadFailed()), SLOT(sourcesLoadFailed()));
309329

310330
// XXX:
311331
for (QObject* o : modules) {

ReactUbuntu/runtime/src/reactbridge.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ReactModuleData;
2626
class ReactUIManager;
2727
class ReactImageLoader;
2828
class ReactEventDispatcher;
29-
29+
class ReactRedboxItem;
3030

3131
class ReactBridgePrivate;
3232
class ReactBridge : public QObject
@@ -76,12 +76,14 @@ class ReactBridge : public QObject
7676
QList<ReactModuleData*> modules() const;
7777
ReactUIManager* uiManager() const;
7878
ReactImageLoader* imageLoader() const;
79+
ReactRedboxItem* redbox();
7980

8081
Q_SIGNALS:
8182
void readyChanged();
8283

8384
private Q_SLOTS:
8485
void sourcesFinished();
86+
void sourcesLoadFailed();
8587
void applicationScriptDone();
8688

8789
private:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
/**
3+
* Copyright (C) 2016, Canonical Ltd.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*
10+
* Author: Justin McPherson <justin.mcpherson@canonical.com>
11+
*
12+
*/
13+
14+
#include "reactexceptionsmanager.h"
15+
16+
#include "reactbridge.h"
17+
#include "reactredboxitem.h"
18+
19+
20+
class ReactExceptionsManagerPrivate {
21+
public:
22+
ReactBridge* bridge = nullptr;
23+
};
24+
25+
26+
void ReactExceptionsManager::reportSoftException(
27+
const QString& message,
28+
const QList<QVariantMap>& stack,
29+
int exceptionId
30+
) {
31+
d_func()->bridge->redbox()->showErrorMessage(message, stack);
32+
}
33+
34+
void ReactExceptionsManager::reportFatalException(
35+
const QString& message,
36+
const QList<QVariantMap>& stack,
37+
int exceptionId
38+
) {
39+
d_func()->bridge->redbox()->showErrorMessage(message, stack);
40+
41+
// XXX: leaving out reload attempts for now
42+
}
43+
44+
void ReactExceptionsManager::updateExceptionMessage(
45+
const QString& message,
46+
const QList<QVariantMap>& stack,
47+
int exceptionId
48+
) {
49+
d_func()->bridge->redbox()->updateErrorMessage(message, stack);
50+
}
51+
52+
void ReactExceptionsManager::reportUnhandledException(
53+
const QString& message,
54+
const QList<QVariantMap>& stack
55+
) {
56+
reportFatalException(message, stack, 1);
57+
}
58+
59+
60+
ReactExceptionsManager::ReactExceptionsManager(QObject* parent)
61+
: QObject(parent)
62+
, d_ptr(new ReactExceptionsManagerPrivate)
63+
{
64+
}
65+
66+
ReactExceptionsManager::~ReactExceptionsManager()
67+
{
68+
}
69+
70+
void ReactExceptionsManager::setBridge(ReactBridge* bridge)
71+
{
72+
Q_D(ReactExceptionsManager);
73+
d->bridge = bridge;
74+
}
75+
76+
ReactViewManager* ReactExceptionsManager::viewManager()
77+
{
78+
return nullptr;
79+
}
80+
81+
QString ReactExceptionsManager::moduleName()
82+
{
83+
return "RCTExceptionsManager";
84+
}
85+
86+
QList<ReactModuleMethod*> ReactExceptionsManager::methodsToExport()
87+
{
88+
return QList<ReactModuleMethod*>{};
89+
}
90+
91+
QVariantMap ReactExceptionsManager::constantsToExport()
92+
{
93+
return QVariantMap{};
94+
}
95+
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
/**
3+
* Copyright (C) 2016, Canonical Ltd.
4+
* All rights reserved.
5+
*
6+
* This source code is licensed under the BSD-style license found in the
7+
* LICENSE file in the root directory of this source tree. An additional grant
8+
* of patent rights can be found in the PATENTS file in the same directory.
9+
*
10+
* Author: Justin McPherson <justin.mcpherson@canonical.com>
11+
*
12+
*/
13+
14+
#ifndef REACTEXCEPTIONSMANAGER_H
15+
#define REACTEXCEPTIONSMANAGER_H
16+
17+
#include <QByteArray>
18+
#include <QUrl>
19+
20+
#include "reactmoduleinterface.h"
21+
22+
23+
class QNetworkAccessManager;
24+
25+
26+
class ReactExceptionsManagerPrivate;
27+
class ReactExceptionsManager
28+
: public QObject
29+
, public ReactModuleInterface
30+
{
31+
Q_OBJECT
32+
// Q_PLUGIN_METADATA(IID ReactModuleInterface_IID)
33+
Q_INTERFACES(ReactModuleInterface)
34+
35+
Q_INVOKABLE void reportSoftException(
36+
const QString& message,
37+
const QList<QVariantMap>& stack,
38+
int exceptionId);
39+
40+
Q_INVOKABLE void reportFatalException(
41+
const QString& message,
42+
const QList<QVariantMap>& stack,
43+
int exceptionId);
44+
45+
Q_INVOKABLE void updateExceptionMessage(
46+
const QString& message,
47+
const QList<QVariantMap>& stack,
48+
int exceptionId);
49+
50+
Q_INVOKABLE void reportUnhandledException(
51+
const QString& message,
52+
const QList<QVariantMap>& stack);
53+
54+
Q_DECLARE_PRIVATE(ReactExceptionsManager)
55+
56+
public:
57+
ReactExceptionsManager(QObject* parent = 0);
58+
~ReactExceptionsManager();
59+
60+
void setBridge(ReactBridge* bridge) override;
61+
62+
// TODO: this doesnt seem right
63+
ReactViewManager* viewManager() override;
64+
65+
QString moduleName() override;
66+
QList<ReactModuleMethod*> methodsToExport() override;
67+
QVariantMap constantsToExport() override;
68+
69+
private:
70+
QScopedPointer<ReactExceptionsManagerPrivate> d_ptr;
71+
};
72+
73+
#endif // REACTEXCEPTIONSMANAGER_H

0 commit comments

Comments
 (0)
X Tutup