Skip to content

Commit 7d30c4d

Browse files
authored
Merge pull request reduxjs#4055 from reduxjs/feature/update-error-messages
Former-commit-id: 836784f
2 parents 9ed9917 + 6206636 commit 7d30c4d

10 files changed

+101
-27
lines changed

.codesandbox/ci.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sandboxes": [
3+
"vanilla",
4+
"vanilla-ts"
5+
],
6+
"node": "14"
7+
}

errors.json

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,19 +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.",
18-
"16": "Super expression must either be null or a function"
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\"?"
1919
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d8b8502a52b248ac7e5ebc22f33a4763e05af8b2
1+
2c4ab333936364bf77130db1435ed44fbb4ccbce

src/bindActionCreators.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
ActionCreator,
55
ActionCreatorsMapObject
66
} from './types/actions'
7+
import { kindOf } from './utils/kindOf'
78

89
function bindActionCreator<A extends AnyAction = AnyAction>(
910
actionCreator: ActionCreator<A>,
@@ -64,9 +65,9 @@ export default function bindActionCreators(
6465

6566
if (typeof actionCreators !== 'object' || actionCreators === null) {
6667
throw new Error(
67-
`bindActionCreators expected an object or a function, instead received ${
68-
actionCreators === null ? 'null' : typeof actionCreators
69-
}. ` +
68+
`bindActionCreators expected an object or a function, but instead received: '${kindOf(
69+
actionCreators
70+
)}'. ` +
7071
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
7172
)
7273
}

src/combineReducers.ts.REMOVED.git-id

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0da94c9b85327e579d0082c6c26137f4d973c0c2
1+
2de2e55bd890e8f8a7ee4f2f1215b7a787e6e4ec

src/createStore.ts.REMOVED.git-id

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3a980bba37bacd3f9ac670ba9db2fcda6c5cdb83
1+
73b27c1be652de0e722106efe07f93f5c418d83b

src/utils/kindOf.ts

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
export function kindOf(val: any): string {
2+
let typeOfVal: string = 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: any) {
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+
}
20+
21+
if (Array.isArray(val)) return 'array'
22+
if (isDate(val)) return 'date'
23+
if (isError(val)) return 'error'
24+
25+
const constructorName = ctorName(val)
26+
switch (constructorName) {
27+
case 'Symbol':
28+
case 'Promise':
29+
case 'WeakMap':
30+
case 'WeakSet':
31+
case 'Map':
32+
case 'Set':
33+
return constructorName
34+
}
35+
36+
// other
37+
return type.slice(8, -1).toLowerCase().replace(/\s/g, '')
38+
}
39+
40+
function ctorName(val: any): string | null {
41+
return typeof val.constructor === 'function' ? val.constructor.name : null
42+
}
43+
44+
function isError(val: any) {
45+
return (
46+
val instanceof Error ||
47+
(typeof val.message === 'string' &&
48+
val.constructor &&
49+
typeof val.constructor.stackTraceLimit === 'number')
50+
)
51+
}
52+
53+
function isDate(val: any) {
54+
if (val instanceof Date) return true
55+
return (
56+
typeof val.toDateString === 'function' &&
57+
typeof val.getDate === 'function' &&
58+
typeof val.setDate === 'function'
59+
)
60+
}
61+
62+
typeOfVal = miniKindOf(val)
63+
}
64+
65+
return typeOfVal
66+
}

test/bindActionCreators.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,17 @@ describe('bindActionCreators', () => {
7777
expect(() => {
7878
bindActionCreators(undefined, store.dispatch)
7979
}).toThrow(
80-
'bindActionCreators expected an object or a function, instead received undefined. ' +
81-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
80+
`bindActionCreators expected an object or a function, but instead received: 'undefined'. ` +
81+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
8282
)
8383
})
8484

8585
it('throws for a null actionCreator', () => {
8686
expect(() => {
8787
bindActionCreators(null, store.dispatch)
8888
}).toThrow(
89-
'bindActionCreators expected an object or a function, instead received null. ' +
90-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
89+
`bindActionCreators expected an object or a function, but instead received: 'null'. ` +
90+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
9191
)
9292
})
9393

@@ -98,8 +98,8 @@ describe('bindActionCreators', () => {
9898
store.dispatch
9999
)
100100
}).toThrow(
101-
'bindActionCreators expected an object or a function, instead received string. ' +
102-
'Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?'
101+
`bindActionCreators expected an object or a function, but instead received: 'string'. ` +
102+
`Did you write "import ActionCreators from" instead of "import * as ActionCreators from"?`
103103
)
104104
})
105105
})
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
87dc94fb1b1c28380de657c4df0f41d14a1c9f91
1+
59786a1c8429828c8303f9b859622982997363bc
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
e422c73c434f0bb6184a6f9043b3a205796bcf08
1+
c9e1143bb76552060055fe1ce29b57a8670465b1

0 commit comments

Comments
 (0)