Skip to content

Commit c1f043a

Browse files
committed
Refactor error notifying
1 parent fdb5407 commit c1f043a

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/browser/extension/inject/pageScript.js

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { stringify } from 'circular-json';
22
import configureStore from '../../../app/store/configureStore';
33
import { isAllowed } from '../options/syncOptions';
4+
import notifyErrors from '../utils/notifyErrors';
45

56
window.devToolsExtension = function(next) {
67
let store = {};
@@ -97,7 +98,15 @@ window.devToolsExtension = function(next) {
9798

9899
function init() {
99100
window.addEventListener('message', onMessage, false);
100-
window.devToolsExtension.notifyErrors(store, relay, () => { errorOccurred = true; });
101+
notifyErrors(() => {
102+
errorOccurred = true;
103+
const state = store.liftedStore.getState();
104+
if (state.computedStates[state.currentStateIndex].error) {
105+
relay('STATE', state);
106+
return false;
107+
}
108+
return true;
109+
});
101110
}
102111

103112
if (next) {
@@ -127,26 +136,4 @@ window.devToolsExtension.open = function(position) {
127136
}, '*');
128137
};
129138

130-
// Catch errors
131-
window.devToolsExtension.notifyErrors = function(store, relay, onError) {
132-
function postError(message) {
133-
if (store && store.liftedStore && relay) {
134-
const state = store.liftedStore.getState();
135-
if (state.computedStates[state.currentStateIndex].error) {
136-
relay('STATE', state);
137-
if (onError) onError();
138-
return;
139-
}
140-
}
141-
window.postMessage({
142-
source: 'redux-page',
143-
type: 'ERROR',
144-
message: message
145-
}, '*');
146-
}
147-
function catchErrors(e) {
148-
if (window.devToolsOptions && !window.devToolsOptions.notifyErrors) return;
149-
postError(e.message);
150-
}
151-
window.addEventListener('error', catchErrors, false);
152-
};
139+
window.devToolsExtension.notifyErrors = notifyErrors;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let handleError;
2+
3+
function postError(message) {
4+
if (handleError && !handleError()) return;
5+
window.postMessage({
6+
source: 'redux-page',
7+
type: 'ERROR',
8+
message: message
9+
}, '*');
10+
}
11+
12+
function catchErrors(e) {
13+
if (window.devToolsOptions && !window.devToolsOptions.notifyErrors) return;
14+
postError(e.message);
15+
}
16+
17+
export default function notifyErrors(onError) {
18+
handleError = onError;
19+
window.addEventListener('error', catchErrors, false);
20+
}

0 commit comments

Comments
 (0)