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

Throw an Error object when there is an authorization or validation failure #15

Closed
OldSneerJaw opened this issue Apr 8, 2018 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@OldSneerJaw
Copy link
Owner

Feature Request

Description

Currently, when a generated sync function throws an authorization or validation error, it does so as a plain object as suggested by the official CouchDB developer docs. For example:

throw { forbidden: 'Unknown document type' };

While this is technically valid JavaScript, it produces a less desirable result when using the test-fixture-maker module to validate document definitions. Specifically, mocha generates a message that can be a little confusing/misleading if an unexpected validation error occurs in a test case. For example:

Error: the object {
  "forbidden": "Invalid hashtableDoc document: hashtable \"staticSizeValidationProp\" must not be smaller than 3 elements"
} was thrown, throw an Error :)

Despite being a cheerful reminder that it is preferable to throw an Error object, the message can be confusing to the user since they may not be aware of that implementation detail. CouchDB interprets all thrown Error objects as 500 Internal Server Error, so validation functions themselves should not throw an Error object. Instead, the test-environment-maker should wrap validation functions in a try-catch block that wraps a plain thrown object in an Error object with its forbidden property set accordingly in such instances.

Examples

In test-environment-template.js:

validationFunction: function(doc, oldDoc, userContext, securityInfo) {
  try {
    ($VALIDATION_FUNC_PLACEHOLDER$)(doc, oldDoc, userContext, securityInfo)
  } catch (error) {
    if (error.forbidden) {
      const wrapperError = new Error(error.forbidden);
      wrapperError.forbidden = error.forbidden;

      throw wrapperError;
    } else {
      throw error;
    }
  }
}
@OldSneerJaw OldSneerJaw added the enhancement New feature or request label Apr 8, 2018
@OldSneerJaw OldSneerJaw self-assigned this Apr 8, 2018
OldSneerJaw added a commit that referenced this issue Apr 8, 2018
…r permissions

Throwing an Error object results in more meaningful error messages when there are unexpected authorization or validation failures in test cases.
@OldSneerJaw
Copy link
Owner Author

Resolved by commit 349baca.

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

No branches or pull requests

1 participant