Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Coffee Machine] Wrong use of resolve in uriVariables #19

Open
egekorkan opened this issue Jun 11, 2020 · 3 comments
Open

[Coffee Machine] Wrong use of resolve in uriVariables #19

egekorkan opened this issue Jun 11, 2020 · 3 comments

Comments

@egekorkan
Copy link
Member

In the coffee machine tutorial, there is the following code:

    thing.setPropertyReadHandler('availableResourceLevel', (options) => {

        // Check if uriVariables are provided
        if (options && typeof options === 'object' && 'uriVariables' in options) {
            const uriVariables: any = options['uriVariables'];
            if ('id' in uriVariables) {
                return thing.readProperty('allAvailableResources').then((resources) => {
                    const id = uriVariables['id'];
                    return new Promise((resolve, reject) => {
                        resolve(resources[id]);
                    });
                });
            }
        }
        return new Promise((resolve, reject) => {
            resolve('Please specify id variable as uriVariables.');
        });
    });

The first part is completely fine but the last lines on returning a Promise will actually make the Client think that everything is OK since in HTTP it would map to 200 status code. The correct way would be to return an HTTP 400 Bad Request but this is not possible at the moment (See this issue on node-wot: eclipse-thingweb/node-wot#140). For now, this should at least be a reject instead of a resolve.

Could you correct this @fatadel ?

@fatadel
Copy link
Contributor

fatadel commented Jun 12, 2020

I had that in the very beginning since it also seems more logical to me. However, I have changed it resolve + error message, since using reject doesn't give any error to a client and the client just infinitely awaits a response. And only on the producer Thing side, the error says (node:2155) UnhandledPromiseRejectionWarning: Please specify id variable as uriVariables. So, I guess there are some additional promise rejection mechanisms missing.

@egekorkan
Copy link
Member Author

Did you try a generic REST client like Postman? It should receive 500 status code

@fatadel
Copy link
Contributor

fatadel commented Jun 13, 2020

I am using Insomnia which is essentially the same as Postman and the result was exactly I have described.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants