Hi,
I was trying to add some custom validation logic in the “beforeCreate” lifecycle hook, but I was not able to customize the error message displayed in the UI , it is always returning the default one.
I tried to throw a “new Error”(“custom message”)" but no luck there.
Is it something doable ?
module.exports = {
lifecycles: {
beforeCreate(data) {
if (someCondition(data)) {
// throw error here, it will prevent the entry creation
throw strapi.errors.badRequest('Some message you want to show in the admin UI');
}
},
},
};
This is great! Works like a charm!
It would be great if this was documented in the official Strapi documentation. @piwi maybe you could add this to the documentation API Reference - Strapi Developer Documentation? thanks!
Thanks for the ping @meck93 !
Lifecycle hooks and error handling will be fully redesigned for Strapi v4, so the documentation will be updated accordingly.
That doesn’t seems to work anymore in Strapi v4, I can’t find anything about this in the v4 docs.
Does anyone have a clue on how to throw custom errors to the backend UI ?
I was able to get an error to appear by adding a global middleware since Strapi v4 allows error handling with the ctx object. Error handling - Strapi Developer Docs
module.exports = (config, { strapi }) => {
// Add your own logic here.
return async (ctx, next) => {
strapi.log.info('In page-adjuster middleware.');
// check that the page content is being changed
if ( (ctx.request.method === 'PUT' || ctx.request.method === 'POST') &&
ctx.request.url.includes('/content-manager/collection-types/api::page.page')) {
// check if the slug is restrictive
if (ctx.request.body.slug === 'test') {
strapi.log.info('restrictive slug');
return ctx.badRequest('Restricted slug: Choose a different slug', { slug: ctx.request.body.slug })
}
}
await next();
};
};
Hi @piwi
What would be the equivalent solution in Strapi v4.0 of the elegant code snippet that @christianguevara has proposed (cf. below) ?
Thanks & Regards,
module.exports = {
lifecycles: {
beforeCreate(data) {
if (someCondition(data)) {
// throw error here, it will prevent the entry creation
throw strapi.errors.badRequest('Some message you want to show in the admin UI');
}
},
},
Thanks @taka
However, does anyone know how to customize the error message in the error snackbar ?
When using @taka solution in a lifecycle hook, the error is thrown as expected but the error message in the snackbar is as usual: “Warning Alert: Bad request”… Is there a way to customize this message to give more details to the user ? I can’t find a solution for strapi v4
for custom notification message, Maybe you need to override Notification component in admin
.cache/admin/src/components/Notifications/Notification/index.js
strapi.errors.badRequest is able to change notification message in single delete of file upload but it doesn’t work in multi delete. Notification msg is not changing and confirm button upload is not getting removed
Using throw new ApplicationError('Error Message Here'); or for that matter any errors that are exposed by @strapi/utils/libs/errors do work as long as they are not used in the beforeDelete hook.
The beforeDelete hook just shows a default message of “Warning An error occurred during record deletion.”.
thanks @taka
i reached to notification component but i want my error msg to be dynamic and provided with the messege written in ApplicationError(""); but in the Notification Component i can’t access this message
Hello there again! Using Strapi v4.10.6. Can’t display error message in beforeDelete hooks, and while throwing Application Error in content-manager/strapi-server.js overwritten update/delete methods. It just shows standart error. Anyone has modern solution to this problem?