eclipse-thingweb / eclipse-thingweb/node-wot

[question] How to return 400 Bad Request status code or similar

Open
#140 3 comments 0 reactions 0 assignees View on GitHub
binding-http question
Dominant language
TypeScript
Stars
192
Forks
100
Avg merge
3d 2h
Merged PRs (30d)
6

Description

For an Exposed Thing implementation, what are different errors we can return from an action invoke, property write etc. that is supported in node-wot?
At the moment, I have the following for an Action Handler:
```
(data) => {
return new Promise((resolve, reject) => {
if (!ajv.validate(this.thing.actions.myAction.input, data)) {
reject(new Error ("Invalid input")); // Need a better error message
} else {
resolve(this.move(speeds));
}
});
});
```
where ajv is a JSON Schema validator that validates the input payload to the input DataSchema. This code returns a 500 error in the case of HTTP Binding which means that there was something wrong in the HTTP Server internally.

I have looked at the Scripting API spec where the [setActionHandler method](https://w3c.github.io/wot-scripting-api/#the-setactionhandler-method) says that we should return [NotSupportedError](https://heycam.github.io/webidl/#notsupportederror) when we don't have a handle. This is not the case here, our case is more related to the ActionHandler itself. The spec says that this case should return an error but does not give details. A close inspection of [different error names in WebIDL](https://heycam.github.io/webidl/#idl-DOMException-error-names), it would be maybe suitable for this specific case to:
* Use [DataError](https://heycam.github.io/webidl/#dataerror). However, this means that the data is inadequate. The word inadequate however has a meaning like "not enough", "insufficient" (see [here](https://www.merriam-webster.com/dictionary/inadequate). This would be not 100% correct since maybe the data is simply wrong, like sending integer instead of string.
* Use [TypeError](https://heycam.github.io/webidl/#exceptiondef-typeerror). However, this would not be valid if the types match (sending an object when an object is asked) but the properties of this object is not correct, like sending `{"hey":"wot"}` whereas the server has a DataSchema like
```
{
"type":"object",
"properties":{
"key":{"type":"string"}
},
"required":["key"]
}
```

I am guessing that this is 80% scripting api issue but there has to be at least a way to return 4XX-like errors in node-wot.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.