Debugging selector functions with errors at runtime?
This might be a poor question with an obvious answer, but it didn't seem super easy to me so I thought I'd ask. I've run into the scenario where I have some function I want to use in a selector that ends up having a runtime error. The problem is, the stack trace doesn't reference the actual selector, instead it references the internals of reselect. This can make it really hard to debug...is there an easy way?
Example:
const mySelector = createSelector(
getSomeStuff,
stuff => {
stuff.map(someOtherFunc);
}
);
Sometime when running, if stuff ends up being undefined you'll get a `cannot read property map of undefined', but a cryptic stack trace that doesn't reference this part of the code, but the memoized functions etc.
Any thoughts?
Running this (node v6.4):
const reselect = require('reselect')
const createSelector = reselect.createSelector
const getSomeStuff = state => state.stuff
const mySelector = createSelector(getSomeStuff, stuff => stuff.map(x => x * 2))
console.log(mySelector({nostuff: [100, 200]}))
Gets me this stack trace which seems reasonable:
/Users/leebannard/projects/javascript/reselect-playground/test.js:9
const mySelector = createSelector(getSomeStuff, stuff => stuff.map(x => x * 2))
^
TypeError: Cannot read property 'map' of undefined
at stuff (/Users/leebannard/projects/javascript/reselect-playground/test.js:9:63)
at /Users/leebannard/projects/javascript/reselect-playground/node_modules/reselect/lib/index.js:67:25
at /Users/leebannard/projects/javascript/reselect-playground/node_modules/reselect/lib/index.js:30:23
at selector (/Users/leebannard/projects/javascript/reselect-playground/node_modules/reselect/lib/index.js:78:33)
at Object.<anonymous> (/Users/leebannard/projects/javascript/reselect-playground/test.js:11:13)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
Is this similar to the stack trace you were getting?
Thanks for the response @ellbee! I get this same result, but unfortunately not with our built application. Perhaps something is up with webpack/source maps. I'll dig deeper but looks like it's probably not a reselect issue.
I have done more digging into one example that has plagued our project and it involves the use of Ramda, or not using anonymous functions as the last function in a createSelector. You can see what I mean with this code:
const reselect = require('reselect')
const createSelector = reselect.createSelector
const getSomeStuff = state => state.stuff
const ramdaMap = mapFunc => arr => arr.map(mapFunc);
const testFunc = ramdaMap(x => x * 2);
const mySelector = createSelector(getSomeStuff, testFunc );
console.log(mySelector({nostuff: [100, 200]}))
Although my "implementation" of ramdaMap is not the same as in ramda, the stack trace is equally cryptic:
/Users/nick/Dev/reselectTest/test.js:7
const ramdaMap = mapFunc => arr => arr.map(mapFunc);
^
TypeError: Cannot read property 'map' of undefined
at arr (/Users/nick/Dev/reselectTest/test.js:7:39)
at /Users/nick/node_modules/reselect/lib/index.js:67:25
at /Users/nick/node_modules/reselect/lib/index.js:30:23
at selector (/Users/nick/node_modules/reselect/lib/index.js:78:33)
at Object.<anonymous> (/Users/nick/Dev/reselectTest/test.js:11:13)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
While in this case it's obvious where the problem is, if you use the same RamdaMap (or R.map) in many many selectors and couple that with react/redux/react-redux it becomes very difficult to track down the offending selector. Any suggestions? We are trying to be vigilante and use things like R.propOr, R.pathOr to ensure we don't end up with undefined values but it does happen from time to time.
@NicholasGWK please have a look at #191, do you think that would help?
Great stuff! I think something like this would be a huge help for dev.