Skip to content

Commit 85a2882

Browse files
committed
Improved error messages
1 parent 0ebf308 commit 85a2882

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

src/core/action.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,18 @@ export function isStrictModeEnabled(): boolean {
8989
}
9090

9191
export function allowStateChanges<T>(allowStateChanges: boolean, func: () => T): T {
92+
// TODO: deprecate / refactor this function in next major
93+
// Currently only used by `@observer`
94+
// Proposed change: remove first param, rename to `forbidStateChanges`,
95+
// require error callback instead of the hardcoded error message now used
96+
// Use `inAction` instead of allowStateChanges in derivation.ts to check strictMode
9297
const prev = allowStateChangesStart(allowStateChanges);
93-
const res = func();
94-
allowStateChangesEnd(prev);
98+
let res;
99+
try {
100+
res = func();
101+
} finally {
102+
allowStateChangesEnd(prev);
103+
}
95104
return res;
96105
}
97106

src/core/derivation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export function checkIfStateModificationsAreAllowed(atom: IAtom) {
109109
fail(getMessage("m031") + atom.name);
110110
// Should not be possible to change observed state outside strict mode, except during initialization, see #563
111111
if (!globalState.allowStateChanges && atom.observers.length > 0)
112-
fail(getMessage(globalState.strictMode ? "m030" : "m030") + atom.name);
112+
fail(getMessage(globalState.strictMode ? "m030a" : "m030b") + atom.name);
113113
}
114114

115115
/**

src/utils/messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const messages = {
2828
"m028" : "It is not allowed to set `useStrict` when a derivation is running" ,
2929
"m029" : "INTERNAL ERROR only onBecomeUnobserved shouldn't be called twice in a row" ,
3030
"m030" : "Since strict-mode is enabled, changing observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: " ,
31+
"m030a" : "Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: " ,
32+
"m030b" : "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: " ,
3133
"m031" : "Computed values are not allowed to not cause side effects by changing observables that are already being observed. Tried to modify: ",
3234
"m032" : "* This computation is suspended (not in use by any reaction) and won't run automatically.\n Didn't expect this computation to be suspended at this point?\n 1. Make sure this computation is used by a reaction (reaction, autorun, observer).\n 2. Check whether you are using this computation synchronously (in the same stack as they reaction that needs it).",
3335
"m033" : "`observe` doesn't support the fire immediately property for observable maps." ,

test/strict-mode.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var test = require('tape');
22
var mobx = require('../');
33

4-
var strictError = /Since strict-mode is enabled, changing observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: /;
4+
var strictError = /Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: /;
55

66
test('strict mode should not allow changes outside action', t => {
77
var a = mobx.observable(2);
@@ -189,7 +189,7 @@ test('strict mode checks', function(t) {
189189
mobx.extras.allowStateChanges(false, function() {
190190
x.set(4);
191191
});
192-
});
192+
}, /Side effects like changing state are not allowed at this point/);
193193

194194
mobx.extras.resetGlobalState();
195195
d()

0 commit comments

Comments
 (0)