parse-server icon indicating copy to clipboard operation
parse-server copied to clipboard

Cloud validation: Custom error handling instead of throwing errors

Open vince1995 opened this issue 3 years ago • 1 comments
trafficstars

New Feature / Enhancement Checklist

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

vince1995 avatar Sep 21 '22 07:09 vince1995

Thanks for opening this issue!

  • 🎉 We are excited about your ideas for improvement!