Use ApplicationError for validation

This commit is contained in:
Rémi de Juvigny 2023-04-21 10:24:22 +02:00
parent 70e60071e4
commit 8ae384894e
2 changed files with 21 additions and 8 deletions

View File

@ -1,5 +1,6 @@
'use strict';
const { ApplicationError } = require('@strapi/utils').errors;
const createFieldSizesService = require('../field-sizes');
const strapi = {
@ -46,14 +47,24 @@ describe('field sizes service', () => {
it('should throw an error if the type is not found', () => {
const { getFieldSize } = createFieldSizesService({ strapi });
expect(() => getFieldSize('not-found')).toThrowError(
'Could not find field size for type not-found'
);
try {
getFieldSize('not-found');
} catch (error) {
expect(error instanceof ApplicationError).toBe(true);
expect(error.message).toBe('Could not find field size for type not-found');
}
});
it('should throw an error if the type is not provided', () => {
const { getFieldSize } = createFieldSizesService({ strapi });
expect(() => getFieldSize()).toThrowError('The type is required');
try {
getFieldSize();
} catch (error) {
expect(error instanceof ApplicationError).toBe(true);
expect(error.message).toBe('The type is required');
}
});
it('should set the custom fields input sizes', () => {

View File

@ -1,5 +1,7 @@
'use strict';
const { ApplicationError } = require('@strapi/utils').errors;
const needsFullSize = {
default: 12,
isResizable: false,
@ -52,12 +54,12 @@ const createFieldSizesService = ({ strapi }) => {
getFieldSize(type) {
if (!type) {
throw new Error('The type is required');
throw new ApplicationError('The type is required');
}
const fieldSize = fieldSizes[type];
if (!fieldSize) {
throw new Error(`Could not find field size for type ${type}`);
throw new ApplicationError(`Could not find field size for type ${type}`);
}
return fieldSize;
@ -65,11 +67,11 @@ const createFieldSizesService = ({ strapi }) => {
setFieldSize(type, size) {
if (!type) {
throw new Error('The type is required');
throw new ApplicationError('The type is required');
}
if (!size) {
throw new Error('The size is required');
throw new ApplicationError('The size is required');
}
fieldSizes[type] = size;