Skip to content

Commit 515e3a7

Browse files
Merge pull request #259 from usu/fix/error-message-improvement
chore: error message improvement
2 parents 4a6d1c9 + 2ae3683 commit 515e3a7

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-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
}

tests/store.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,42 @@ describe('API store', () => {
14671467
expect(vm.api.get('/camps/1').emptyArray).toEqual([])
14681468
done()
14691469
})
1470+
1471+
it('throws error when accessing non-existing property like a relation', async done => {
1472+
// given
1473+
axiosMock.onGet('http://localhost/').reply(200, root.serverResponse)
1474+
1475+
// when
1476+
let loadingObject = null
1477+
loadingObject = vm.api.get().nonexistingProperty()
1478+
1479+
// then (loading)
1480+
expect(loadingObject).toBeInstanceOf(LoadingStoreValue)
1481+
expect(loadingObject.toJSON()).toEqual('{}')
1482+
1483+
// then (loaded)
1484+
await expect(loadingObject._meta.load).rejects.toThrow("Property 'nonexistingProperty' on resource http://localhost was used like a relation, but no relation with this name was returned by the API (actual return value: undefined)")
1485+
1486+
done()
1487+
})
1488+
1489+
it('throws error when accessing primitive property like a relation', async done => {
1490+
// given
1491+
axiosMock.onGet('http://localhost/').reply(200, root.serverResponse)
1492+
1493+
// when
1494+
let loadingObject = null
1495+
loadingObject = vm.api.get().the()
1496+
1497+
// then (loading)
1498+
expect(loadingObject).toBeInstanceOf(LoadingStoreValue)
1499+
expect(loadingObject.toJSON()).toEqual('{}')
1500+
1501+
// then (loaded)
1502+
await expect(loadingObject._meta.load).rejects.toThrow("Property 'the' on resource http://localhost was used like a relation, but no relation with this name was returned by the API (actual return value: \"root\")")
1503+
1504+
done()
1505+
})
14701506
})
14711507
})
14721508
})

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)