Handle errors when sending custom field from the admin to the server

This commit is contained in:
Mark Kaylor 2022-08-02 16:16:35 +02:00
parent 6a5f4a97b9
commit ebe194db77

View File

@ -8,6 +8,21 @@
const convertCustomFieldType = attributes => {
Object.values(attributes).forEach(attribute => {
if (attribute.customField) {
if (['component', 'relation', 'dynamiczone'].includes(attribute.type)) {
// When the registered custom fiel is using an invalid strapi type
throw new Error(`Type: ${attribute.type} is not a valid Custom Field type`);
}
// Use the custom field uid sent from the admin to get its equivalent on the server
// The getter will throw an error if the custom field is not found
const customField = strapi.container.get('custom-fields').get(attribute.customField);
if (customField.type !== attribute.type) {
// When there is a type mismatch between admin and server
throw new Error(
`Custom field: "${attribute.customField}" sent type: "${attribute.type}" from the admin but the server expected type: "${customField.type}"`
);
}
attribute.type = 'customField';
}
});