Syntax errors should dismiss redboxes

Summary: If you make a syntax error while there is a redbox while Fast Refresh is on, we should dismiss that redbox. Otherwise there is no way for you to tell why your code is not working.

Reviewed By: rickhanlonii

Differential Revision: D15970337

fbshipit-source-id: 1ca6c9a1b2269d198ae726d3b64e5c51506503db
This commit is contained in:
Dan Abramov
2019-06-24 13:54:40 -07:00
committed by Facebook Github Bot
parent 0a17699fd5
commit 7d2a95d43d
+24 -17
View File
@@ -139,7 +139,7 @@ Error: ${e.message}`;
});
// This is intentionally called lazily, as these values change.
function shouldProvideVisualFeedback() {
function isFastRefreshActive() {
return (
// Until we get "connection-done", messages aren't real edits.
didFinishInitialUpdate &&
@@ -151,27 +151,31 @@ Error: ${e.message}`;
);
}
function dismissRedbox() {
if (
Platform.OS === 'ios' &&
NativeRedBox != null &&
NativeRedBox.dismiss != null
) {
NativeRedBox.dismiss();
} else {
const NativeExceptionsManager = require('../Core/NativeExceptionsManager')
.default;
NativeExceptionsManager &&
NativeExceptionsManager.dismissRedbox &&
NativeExceptionsManager.dismissRedbox();
}
}
hmrClient.on('update-start', () => {
if (shouldProvideVisualFeedback()) {
if (isFastRefreshActive()) {
HMRLoadingView.showMessage('Refreshing...');
}
});
hmrClient.on('update', () => {
if (shouldProvideVisualFeedback()) {
if (
Platform.OS === 'ios' &&
NativeRedBox != null &&
NativeRedBox.dismiss != null
) {
NativeRedBox.dismiss();
} else {
const NativeExceptionsManager = require('../Core/NativeExceptionsManager')
.default;
NativeExceptionsManager &&
NativeExceptionsManager.dismissRedbox &&
NativeExceptionsManager.dismissRedbox();
}
if (isFastRefreshActive()) {
dismissRedbox();
}
});
@@ -192,7 +196,10 @@ Error: ${e.message}`;
setHMRUnavailableReason(
'The Metro server and the client are out of sync. Fast Refresh will be disabled until you reload the application.',
);
} else {
} else if (isFastRefreshActive()) {
// Even if there is already a redbox, syntax errors are more important.
// Otherwise you risk seeing a stale runtime error while a syntax error is more recent.
dismissRedbox();
throw new Error(`${data.type} ${data.message}`);
}
});