in Strapi 4.0, i want to validate the input before saving. So i created lifecycles.js file as per the documentation and added the code:
module.exports = {
beforeCreate(event) {
//validation login here;
if (!valid) {
throw strapi.errors.badRequest('Invalid Entry');
}
},
}
How ever throw strapi.errors.badRequest('Invalid Entry');
is giving an error :
Cannot read property 'badRequest' of undefined
My guess is the Strapi v4 changed it from version 3. I looked everywhere but couldn't find a solution.
Any idea on how to handle error in lifecycles.js
?
I had a similar situation with a forbidden error. I got to do it importing a class from @strapi/utils/lib/errors.js
const { ForbiddenError } = require("@strapi/utils").errors;
...
if (!authorized) {
throw new ForbiddenError(errorMessage);
}