actions-on-google / actions-on-google-nodejs

Node.js client library for Actions on Google
https://actions-on-google.github.io/actions-on-google-nodejs
Apache License 2.0
900 stars 194 forks source link

Using smarthome, how to change status code? #404

Closed cesarjhony closed 4 years ago

cesarjhony commented 4 years ago

I'm using smarthome from actions-on-google-nodejs and when app.onSync(handler) thows a exception, then the status code is 500. I see that smarthome should send 401 on internal error, but it is not happen, however I need to know how to properly set an arbitrary status code. I tried throw new UnauthorizedError(); but don't change the code. I tried handler(body, headers, raw){ return raw.express.response.status(401).send() } and its variations with ...end(), just status(401), then works but throw: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

I am using mocha 7.2.0, chai 4.2.0 with node 10.16.0 for tests, the same problem occurs on Heroku with node 12. My project is wrote in Javascript ( not typeScript )

Fleker commented 4 years ago

For smart home you should not send a 401. Rather you should send a 200 with a specific error message in the payload:

{
  "requestId": "12345",
  "payload": {
    "errorCode": "inSoftwareUpdate",
    "status" : "ERROR"
  }
}
cesarjhony commented 4 years ago

In process to check user the Docs in here specify the server should return 401 error code when token was not found or expired. That verification need to be done in a middleware to throw 401?

Thank you for your response and reference link.

tilfin commented 3 years ago

If you wrap smarthome handler by any function that verifies an access token, you can throw 401 status code but don't have how to pass the auth user into onXxxx callbacks.