Skip to content

Commit 46f5c94

Browse files
committed
Port error message updates from master
1 parent b3e26c6 commit 46f5c94

8 files changed

+176
-56
lines changed

errors.json

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
{
2-
"0": "It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function.",
3-
"1": "Expected the enhancer to be a function.",
4-
"2": "Expected the reducer to be a function.",
2+
"0": "It looks like you are passing several store enhancers to createStore(). This is not supported. Instead, compose them together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.",
3+
"1": "Expected the enhancer to be a function. Instead, received: ''",
4+
"2": "Expected the root reducer to be a function. Instead, received: ''",
55
"3": "You may not call store.getState() while the reducer is executing. The reducer has already received the state as an argument. Pass it down from the top reducer instead of reading it from the store.",
6-
"4": "Expected the listener to be a function.",
6+
"4": "Expected the listener to be a function. Instead, received: ''",
77
"5": "You may not call store.subscribe() while the reducer is executing. If you would like to be notified after the store has been updated, subscribe from a component and invoke store.getState() in the callback to access the latest state. See https://redux.js.org/api/store#subscribelistener for more details.",
88
"6": "You may not unsubscribe from a store listener while the reducer is executing. See https://redux.js.org/api/store#subscribelistener for more details.",
9-
"7": "Actions must be plain objects. Use custom middleware for async actions.",
10-
"8": "Actions may not have an undefined \"type\" property. Have you misspelled a constant?",
9+
"7": "Actions must be plain objects. Instead, the actual type was: ''. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.",
10+
"8": "Actions may not have an undefined \"type\" property. You may have misspelled an action type string constant.",
1111
"9": "Reducers may not dispatch actions.",
12-
"10": "Expected the nextReducer to be a function.",
13-
"11": "Expected the observer to be an object.",
14-
"12": "bindActionCreators expected an object or a function, instead received . Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?",
15-
"13": "Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.",
16-
"14": "Reducer \"\" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.",
17-
"15": "Reducer \"\" returned undefined when probed with a random type. Don't try to handle or other actions in \"redux/*\" namespace. They are considered private. Instead, you must return the current state for any unknown actions, unless it is undefined, in which case you must return the initial state, regardless of the action type. The initial state may not be undefined, but can be null."
12+
"10": "Expected the nextReducer to be a function. Instead, received: '",
13+
"11": "Expected the observer to be an object. Instead, received: ''",
14+
"12": "The slice reducer for key \"\" returned undefined during initialization. If the state passed to the reducer is undefined, you must explicitly return the initial state. The initial state may not be undefined. If you don't want to set a value for this reducer, you can use null instead of undefined.",
15+
"13": "The slice reducer for key \"\" returned undefined when probed with a random type. Don't try to handle '' or other actions in \"redux/*\" namespace. They are considered private. Instead, you must return the current state for any unknown actions, unless it is undefined, in which case you must return the initial state, regardless of the action type. The initial state may not be undefined, but can be null.",
16+
"14": "When called with an action of type , the slice reducer for key \"\" returned undefined. To ignore an action, you must explicitly return the previous state. If you want this reducer to hold no value, you can return null instead of undefined.",
17+
"15": "Dispatching while constructing your middleware is not allowed. Other middleware would not be applied to this dispatch.",
18+
"16": "bindActionCreators expected an object or a function, but instead received: ''. Did you write \"import ActionCreators from\" instead of \"import * as ActionCreators from\"?"
1819
}

src/bindActionCreators.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { kindOf } from './utils/kindOf'
2+
13
function bindActionCreator(actionCreator, dispatch) {
24
return function () {
35
return dispatch(actionCreator.apply(this, arguments))
@@ -32,9 +34,9 @@ export default function bindActionCreators(actionCreators, dispatch) {
3234

3335
if (typeof actionCreators !== 'object' || actionCreators === null) {
3436
throw new Error(
35-
`bindActionCreators expected an object or a function, instead received ${
36-
actionCreators === null ? 'null' : typeof actionCreators
37-
}. ` +
37+
`bindActionCreators expected an object or a function, but instead received: '${kindOf(
38+
actionCreators
39+
)}'. ` +
3840
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
3941
)
4042
}

src/combineReducers.js

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import ActionTypes from './utils/actionTypes'
22
import warning from './utils/warning'
33
import isPlainObject from './utils/isPlainObject'
4-
5-
function getUndefinedStateErrorMessage(key, action) {
6-
const actionType = action && action.type
7-
const actionDescription =
8-
(actionType && `action "${String(actionType)}"`) || 'an action'
9-
10-
return (
11-
`Given ${actionDescription}, reducer "${key}" returned undefined. ` +
12-
`To ignore an action, you must explicitly return the previous state. ` +
13-
`If you want this reducer to hold no value, you can return null instead of undefined.`
14-
)
15-
}
4+
import { kindOf } from './utils/kindOf'
165

176
function getUnexpectedStateShapeWarningMessage(
187
inputState,
@@ -35,9 +24,9 @@ function getUnexpectedStateShapeWarningMessage(
3524

3625
if (!isPlainObject(inputState)) {
3726
return (
38-
`The ${argumentName} has unexpected type of "` +
39-
{}.toString.call(inputState).match(/\s([a-z|A-Z]+)/)[1] +
40-
`". Expected argument to be an object with the following ` +
27+
`The ${argumentName} has unexpected type of "${kindOf(
28+
inputState
29+
)}". Expected argument to be an object with the following ` +
4130
`keys: "${reducerKeys.join('", "')}"`
4231
)
4332
}
@@ -69,7 +58,7 @@ function assertReducerShape(reducers) {
6958

7059
if (typeof initialState === 'undefined') {
7160
throw new Error(
72-
`Reducer "${key}" returned undefined during initialization. ` +
61+
`The slice reducer for key "${key}" returned undefined during initialization. ` +
7362
`If the state passed to the reducer is undefined, you must ` +
7463
`explicitly return the initial state. The initial state may ` +
7564
`not be undefined. If you don't want to set a value for this reducer, ` +
@@ -83,8 +72,8 @@ function assertReducerShape(reducers) {
8372
}) === 'undefined'
8473
) {
8574
throw new Error(
86-
`Reducer "${key}" returned undefined when probed with a random type. ` +
87-
`Don't try to handle ${ActionTypes.INIT} or other actions in "redux/*" ` +
75+
`The slice reducer for key "${key}" returned undefined when probed with a random type. ` +
76+
`Don't try to handle '${ActionTypes.INIT}' or other actions in "redux/*" ` +
8877
`namespace. They are considered private. Instead, you must return the ` +
8978
`current state for any unknown actions, unless it is undefined, ` +
9079
`in which case you must return the initial state, regardless of the ` +
@@ -167,8 +156,14 @@ export default function combineReducers(reducers) {
167156
const previousStateForKey = state[key]
168157
const nextStateForKey = reducer(previousStateForKey, action)
169158
if (typeof nextStateForKey === 'undefined') {
170-
const errorMessage = getUndefinedStateErrorMessage(key, action)
171-
throw new Error(errorMessage)
159+
const actionType = action && action.type
160+
throw new Error(
161+
`When called with an action of type ${
162+
actionType ? `"${String(actionType)}"` : '(unknown type)'
163+
}, the slice reducer for key "${key}" returned undefined. ` +
164+
`To ignore an action, you must explicitly return the previous state. ` +
165+
`If you want this reducer to hold no value, you can return null instead of undefined.`
166+
)
172167
}
173168
nextState[key] = nextStateForKey
174169
hasChanged = hasChanged || nextStateForKey !== previousStateForKey

src/createStore.js

+31-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import $$observable from 'symbol-observable'
22

33
import ActionTypes from './utils/actionTypes'
44
import isPlainObject from './utils/isPlainObject'
5+
import { kindOf } from './utils/kindOf'
56

67
/**
78
* Creates a Redux store that holds the state tree.
@@ -36,7 +37,7 @@ export default function createStore(reducer, preloadedState, enhancer) {
3637
throw new Error(
3738
'It looks like you are passing several store enhancers to ' +
3839
'createStore(). This is not supported. Instead, compose them ' +
39-
'together to a single function.'
40+
'together to a single function. See https://redux.js.org/tutorials/fundamentals/part-4-store#creating-a-store-with-enhancers for an example.'
4041
)
4142
}
4243

@@ -47,14 +48,22 @@ export default function createStore(reducer, preloadedState, enhancer) {
4748

4849
if (typeof enhancer !== 'undefined') {
4950
if (typeof enhancer !== 'function') {
50-
throw new Error('Expected the enhancer to be a function.')
51+
throw new Error(
52+
`Expected the enhancer to be a function. Instead, received: '${kindOf(
53+
enhancer
54+
)}'`
55+
)
5156
}
5257

5358
return enhancer(createStore)(reducer, preloadedState)
5459
}
5560

5661
if (typeof reducer !== 'function') {
57-
throw new Error('Expected the reducer to be a function.')
62+
throw new Error(
63+
`Expected the root reducer to be a function. Instead, received: '${kindOf(
64+
reducer
65+
)}'`
66+
)
5867
}
5968

6069
let currentReducer = reducer
@@ -118,7 +127,11 @@ export default function createStore(reducer, preloadedState, enhancer) {
118127
*/
119128
function subscribe(listener) {
120129
if (typeof listener !== 'function') {
121-
throw new Error('Expected the listener to be a function.')
130+
throw new Error(
131+
`Expected the listener to be a function. Instead, received: '${kindOf(
132+
listener
133+
)}'`
134+
)
122135
}
123136

124137
if (isDispatching) {
@@ -184,15 +197,15 @@ export default function createStore(reducer, preloadedState, enhancer) {
184197
function dispatch(action) {
185198
if (!isPlainObject(action)) {
186199
throw new Error(
187-
'Actions must be plain objects. ' +
188-
'Use custom middleware for async actions.'
200+
`Actions must be plain objects. Instead, the actual type was: '${kindOf(
201+
action
202+
)}'. You may need to add middleware to your store setup to handle dispatching other values, such as 'redux-thunk' to handle dispatching functions. See https://redux.js.org/tutorials/fundamentals/part-4-store#middleware and https://redux.js.org/tutorials/fundamentals/part-6-async-logic#using-the-redux-thunk-middleware for examples.`
189203
)
190204
}
191205

192206
if (typeof action.type === 'undefined') {
193207
throw new Error(
194-
'Actions may not have an undefined "type" property. ' +
195-
'Have you misspelled a constant?'
208+
'Actions may not have an undefined "type" property. You may have misspelled an action type string constant.'
196209
)
197210
}
198211

@@ -228,7 +241,11 @@ export default function createStore(reducer, preloadedState, enhancer) {
228241
*/
229242
function replaceReducer(nextReducer) {
230243
if (typeof nextReducer !== 'function') {
231-
throw new Error('Expected the nextReducer to be a function.')
244+
throw new Error(
245+
`Expected the nextReducer to be a function. Instead, received: '${kindOf(
246+
nextReducer
247+
)}`
248+
)
232249
}
233250

234251
currentReducer = nextReducer
@@ -259,7 +276,11 @@ export default function createStore(reducer, preloadedState, enhancer) {
259276
*/
260277
subscribe(observer) {
261278
if (typeof observer !== 'object' || observer === null) {
262-
throw new TypeError('Expected the observer to be an object.')
279+
throw new TypeError(
280+
`Expected the observer to be an object. Instead, received: '${kindOf(
281+
observer
282+
)}'`
283+
)
263284
}
264285

265286
function observeState() {

src/utils/kindOf.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
export function kindOf(val) {
2+
let typeOfVal = typeof val
3+
4+
if (process.env.NODE_ENV !== 'production') {
5+
// Inlined / shortened version of `kindOf` from https://github.com/jonschlinkert/kind-of
6+
function miniKindOf(val) {
7+
if (val === void 0) return 'undefined'
8+
if (val === null) return 'null'
9+
10+
const type = typeof val
11+
switch (type) {
12+
case 'boolean':
13+
case 'string':
14+
case 'number':
15+
case 'symbol':
16+
case 'function': {
17+
return type
18+
}
19+
default: break;
20+
}
21+
22+
if (Array.isArray(val)) return 'array'
23+
if (isDate(val)) return 'date'
24+
if (isError(val)) return 'error'
25+
26+
const constructorName = ctorName(val)
27+
switch (constructorName) {
28+
case 'Symbol':
29+
case 'Promise':
30+
case 'WeakMap':
31+
case 'WeakSet':
32+
case 'Map':
33+
case 'Set':
34+
return constructorName
35+
default: break;
36+
}
37+
38+
// other
39+
return type.slice(8, -1).toLowerCase().replace(/\s/g, '')
40+
}
41+
42+
function ctorName(val) {
43+
return typeof val.constructor === 'function' ? val.constructor.name : null
44+
}
45+
46+
function isError(val) {
47+
return (
48+
val instanceof Error ||
49+
(typeof val.message === 'string' &&
50+
val.constructor &&
51+
typeof val.constructor.stackTraceLimit === 'number')
52+
)
53+
}
54+
55+
function isDate(val) {
56+
if (val instanceof Date) return true
57+
return (
58+
typeof val.toDateString === 'function' &&
59+
typeof val.getDate === 'function' &&
60+
typeof val.setDate === 'function'
61+
)
62+
}
63+
64+
typeOfVal = miniKindOf(val)
65+
}
66+
67+
return typeOfVal
68+
}

test/bindActionCreators.spec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,26 @@ describe('bindActionCreators', () => {
7575
expect(() => {
7676
bindActionCreators(undefined, store.dispatch)
7777
}).toThrow(
78-
'bindActionCreators expected an object or a function, instead received undefined. ' +
79-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
78+
`bindActionCreators expected an object or a function, but instead received: 'undefined'. ` +
79+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
8080
)
8181
})
8282

8383
it('throws for a null actionCreator', () => {
8484
expect(() => {
8585
bindActionCreators(null, store.dispatch)
8686
}).toThrow(
87-
'bindActionCreators expected an object or a function, instead received null. ' +
88-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
87+
`bindActionCreators expected an object or a function, but instead received: 'null'. ` +
88+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
8989
)
9090
})
9191

9292
it('throws for a primitive actionCreator', () => {
9393
expect(() => {
9494
bindActionCreators('string', store.dispatch)
9595
}).toThrow(
96-
'bindActionCreators expected an object or a function, instead received string. ' +
97-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
96+
`bindActionCreators expected an object or a function, but instead received: 'string'. ` +
97+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
9898
)
9999
})
100100
})

test/combineReducers.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ describe('Utils', () => {
235235

236236
createStore(reducer, 1)
237237
expect(spy.mock.calls[2][0]).toMatch(
238-
/createStore has unexpected type of "Number".*keys: "foo", "baz"/
238+
/createStore has unexpected type of "number".*keys: "foo", "baz"/
239239
)
240240

241241
reducer({ corge: 2 })
@@ -250,7 +250,7 @@ describe('Utils', () => {
250250

251251
reducer(1)
252252
expect(spy.mock.calls[5][0]).toMatch(
253-
/reducer has unexpected type of "Number".*keys: "foo", "baz"/
253+
/reducer has unexpected type of "number".*keys: "foo", "baz"/
254254
)
255255

256256
spy.mockClear()

0 commit comments

Comments
 (0)