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

Custom responses for failed actions #322

Merged
merged 5 commits into from
Sep 23, 2016

Conversation

kyork-cl
Copy link
Contributor

We have a couple of cases where we need to know why an action failed. We could come up with some work around with the server log or monitored properties, but that is not an ideal way to handle errors.

This is written such that existing callbacks will not be affected. The err object must have a "properties" member for the behavior to be triggered. I also added a test case to validate the Siren response.

@AdamMagaluk
Copy link
Collaborator

To be clear you're proposing in device actions:

TestDriver.prototype.customError = function(cb) {
  cb({statusCode: 401, properties: {message: 'custom error message'}});
}

This would return the following http response:

< HTTP/1.1 401
{
  class: ['action-error'],
  properties: err.properties,
  links: [
    { rel: ['self'], href: env.helpers.url.current() }
  ]
}

Today if anything is passed back in the first parameter to the callback it returns a 500 with no body.

My only issue with the PR is syntax for returning the error. We were thinking about adding a custom error object possibly on the zetta driver that takes a status code and message.

Maybe something like:

TestDriver.prototype.customError = function(cb) {
  cb(new Driver.ActionError(401, 'custom error message'));

 // Also using js Error would also work, could set the message property and assume 500 status code.
 cb(new Error('custom error message')); 

}

@kyork-cl
Copy link
Contributor Author

I'm fine with that approach, I'll make those changes.

There isn't going to be any problem with actions that previously returned a 500 error with no body having a body after this change?

@AdamMagaluk
Copy link
Collaborator

We don't believe so. We'll do some testing with our clients but all so far just look for a non 500 status code.

@kyork-cl
Copy link
Contributor Author

Added the error. Need to update the callback handler to use it still.

env.response.statusCode = statusCode;
env.response.body = {
class: ['action-error'],
properties: err.properties,
Copy link
Collaborator

@AdamMagaluk AdamMagaluk Sep 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should just be properties not err.properties Message isn't getting passed in the two other cases. Line 544

@AdamMagaluk
Copy link
Collaborator

+1 Thanks!

@AdamMagaluk AdamMagaluk merged commit 241e629 into zettajs:master Sep 23, 2016
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

Successfully merging this pull request may close these issues.

2 participants