Skip to content

Commit 7db8d0b

Browse files
committed
improve error message for primitive properties used as a relation/function
1 parent 4a6d1c9 commit 7db8d0b

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "dist/bundle.js",
66
"scripts": {
77
"build": "cross-env NODE_ENV=production webpack",
8+
"build-dev": "cross-env NODE_ENV=development webpack",
89
"clean": "cross-env rimraf dist coverage lib",
910
"coverage": "cross-env npm run build && jest --coverage",
1011
"lint": "cross-env eslint src --ext .js,.ts",
@@ -83,4 +84,4 @@
8384
"webpack-cli": "4.9.1",
8485
"webpack-node-externals": "3.0.0"
8586
}
86-
}
87+
}

src/LoadingStoreValue.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,16 @@ class LoadingStoreValue implements Resource {
5959

6060
// Proxy to all other unknown properties: return a function that yields another LoadingStoreValue
6161
const loadProperty = loadResource.then(resource => resource[prop])
62-
const result = templateParams => new LoadingStoreValue(loadProperty.then(property => property(templateParams)._meta.load))
62+
63+
const result = templateParams => new LoadingStoreValue(loadProperty.then(property => {
64+
try {
65+
return property(templateParams)._meta.load
66+
} catch (e) {
67+
throw new Error(`Property '${prop.toString()}' on resource ${absoluteSelf} was used like a relation, but no relation with this name was returned by the API (actual return value: ${JSON.stringify(property)})`)
68+
}
69+
}
70+
))
71+
6372
result.toString = () => ''
6473
return result
6574
}

webpack.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var path = require('path')
2-
var webpack = require('webpack')
3-
var nodeExternals = require('webpack-node-externals')
1+
const path = require('path')
2+
const webpack = require('webpack')
3+
const nodeExternals = require('webpack-node-externals')
44

55
module.exports = {
66
devtool: 'source-map',
@@ -16,7 +16,7 @@ module.exports = {
1616
plugins: [
1717
new webpack.DefinePlugin({
1818
'process.env': {
19-
NODE_ENV: JSON.stringify('production')
19+
NODE_ENV: JSON.stringify(process.env.NODE_ENV)
2020
}
2121
})
2222
],

0 commit comments

Comments
 (0)