From 7db8d0b7d1328c038cdc140845eeeca98e83ea7c Mon Sep 17 00:00:00 2001 From: Urban Suppiger Date: Sat, 22 Jan 2022 18:44:43 +0100 Subject: [PATCH 1/3] improve error message for primitive properties used as a relation/function --- package.json | 3 ++- src/LoadingStoreValue.ts | 11 ++++++++++- webpack.config.js | 8 ++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 1df15995..af0f9a8b 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "main": "dist/bundle.js", "scripts": { "build": "cross-env NODE_ENV=production webpack", + "build-dev": "cross-env NODE_ENV=development webpack", "clean": "cross-env rimraf dist coverage lib", "coverage": "cross-env npm run build && jest --coverage", "lint": "cross-env eslint src --ext .js,.ts", @@ -83,4 +84,4 @@ "webpack-cli": "4.9.1", "webpack-node-externals": "3.0.0" } -} +} \ No newline at end of file diff --git a/src/LoadingStoreValue.ts b/src/LoadingStoreValue.ts index 30d2130a..759cc43d 100644 --- a/src/LoadingStoreValue.ts +++ b/src/LoadingStoreValue.ts @@ -59,7 +59,16 @@ class LoadingStoreValue implements Resource { // Proxy to all other unknown properties: return a function that yields another LoadingStoreValue const loadProperty = loadResource.then(resource => resource[prop]) - const result = templateParams => new LoadingStoreValue(loadProperty.then(property => property(templateParams)._meta.load)) + + const result = templateParams => new LoadingStoreValue(loadProperty.then(property => { + try { + return property(templateParams)._meta.load + } catch (e) { + 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)})`) + } + } + )) + result.toString = () => '' return result } diff --git a/webpack.config.js b/webpack.config.js index 97ca7cdd..f41fe51d 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,6 @@ -var path = require('path') -var webpack = require('webpack') -var nodeExternals = require('webpack-node-externals') +const path = require('path') +const webpack = require('webpack') +const nodeExternals = require('webpack-node-externals') module.exports = { devtool: 'source-map', @@ -16,7 +16,7 @@ module.exports = { plugins: [ new webpack.DefinePlugin({ 'process.env': { - NODE_ENV: JSON.stringify('production') + NODE_ENV: JSON.stringify(process.env.NODE_ENV) } }) ], From d4e9cbd1e5f13eb37845b12a246ed4a8dedc6310 Mon Sep 17 00:00:00 2001 From: Urban Suppiger Date: Sun, 23 Jan 2022 07:33:10 +0100 Subject: [PATCH 2/3] tests for improved error message --- tests/store.spec.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/store.spec.js b/tests/store.spec.js index c277a14e..c2dc66ad 100644 --- a/tests/store.spec.js +++ b/tests/store.spec.js @@ -1467,6 +1467,42 @@ describe('API store', () => { expect(vm.api.get('/camps/1').emptyArray).toEqual([]) done() }) + + it('throws error when accessing non-existing property like a relation', async done => { + // given + axiosMock.onGet('http://localhost/').reply(200, root.serverResponse) + + // when + let loadingObject = null + loadingObject = vm.api.get().nonexistingProperty() + + // then (loading) + expect(loadingObject).toBeInstanceOf(LoadingStoreValue) + expect(loadingObject.toJSON()).toEqual('{}') + + // then (loaded) + 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)") + + done() + }) + + it('throws error when accessing primite property like a relation', async done => { + // given + axiosMock.onGet('http://localhost/').reply(200, root.serverResponse) + + // when + let loadingObject = null + loadingObject = vm.api.get().the() + + // then (loading) + expect(loadingObject).toBeInstanceOf(LoadingStoreValue) + expect(loadingObject.toJSON()).toEqual('{}') + + // then (loaded) + 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\")") + + done() + }) }) }) }) From 2ae3683cd57718d5f9dfa7156231994c5db02d55 Mon Sep 17 00:00:00 2001 From: Urban Suppiger Date: Sun, 23 Jan 2022 07:35:24 +0100 Subject: [PATCH 3/3] typo --- tests/store.spec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/store.spec.js b/tests/store.spec.js index c2dc66ad..2fc335dd 100644 --- a/tests/store.spec.js +++ b/tests/store.spec.js @@ -1486,7 +1486,7 @@ describe('API store', () => { done() }) - it('throws error when accessing primite property like a relation', async done => { + it('throws error when accessing primitive property like a relation', async done => { // given axiosMock.onGet('http://localhost/').reply(200, root.serverResponse)