parse-server
parse-server copied to clipboard
Cloud validation: Custom error handling instead of throwing errors
trafficstars
New Feature / Enhancement Checklist
- [x] I am not disclosing a vulnerability.
- [x] I am not just asking a question.
- [x] I have searched through existing issues.
Current Limitation
While cloud code validation if a condition doesn't met an error is thrown.
Feature / Enhancement Description
Add a possibility to handle errors of the cloud code validation on our own by passing an afterValidation function. If the function returns anything (or throws), the cloud function won't get executed. The returned data of this function will also returned to the client.
Parse.Cloud.define("coolCloudCode", async req => {
}, {
requireUser: true,
requireAnyUserRoles: [
"supporter",
],
afterValidation: (error) => {
if (error) {
return {
error: {
message: "validation failed"
}
}
}
}
});
Example Use Case
We are returning JSON objects in every of our cloud functions which contain an success or an error object instead of throwing if something fails. We have chosen this style to make sure that errors are only thrown if there are network issues.
Alternatives / Workarounds
Our current solution:
Parse.Cloud.define("generatePromotionCodes", async req => {
if (
!req.master
&&
(await checkUserRole({
user: req.user,
roleNames: [
"supporter",
],
}))
.error
) {
return {
error: {
message: "No master key or not supported user."
}
}
}
});
Would be much easier if we could use the cloud code validation.
3rd Party References
Thanks for opening this issue!
- 🎉 We are excited about your ideas for improvement!