Throwing an error in lifecycle hook gets triggered in try catch but the input still saves (it should be prevented) and the error is not showing as a popup in the UI
I can get the error to console.log so the code works but I expect/want it to show the error in the UI and the script to stop so Strapi won’t save the data that triggered the error.
I tried adding return in the catch block, can’t use break or exit, so what else can I do?
Lifecycles can’t stop the Creation/Updating of an entry by using the return inside it. The return in lifecycle just means “I don’t want to modify any data before creation/update”.
In that case, you should throw errors instead of returns in beforeCreate/beforeUpdate lifecycles.
So the correct approach to stop a creation will be:
beforeCreate(data, model) {
if(data.num < 3) {
throw new Error('Num is not smaller than 3.'); // throwing errors will STOP the creation.
}
},
Also, if you are trying to do simple checks, like check the numbers, you can also define its max/min values directly in Content Manager when editing a Number field.
The same applies to Text fields, you can define regex patterns to do validation in Admin before creation:
Thanks @sunnyson.
I was actually doing that already, but I see my example code snippet was confusing.
What ended up working was moving the code out of the try catch.
I second this – throwing an error results in a crash. Common usecase for beforeCreate() would be to modify the params and perform simple operations.
With the current functionality you need to copy and override the default controller and put the logic there instead, which seems overkill for a lot of use-cases.