From d61ef75869f70ac1dc0118c0a60a1f5145ea5609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Famibelle-Pronzola?= Date: Sun, 2 Apr 2023 12:25:52 +0400 Subject: [PATCH] Replace ValidationError by ApplicationError in services & lifecycles --- src/api/artiste/content-types/artiste/lifecycles.js | 4 ++-- .../content-types/commentaire/lifecycles.js | 8 ++++---- src/api/commentaire/controllers/commentaire.js | 4 ++-- src/api/parole/content-types/parole/lifecycles.js | 10 ++++++---- src/api/parole/services/parole.js | 8 ++++---- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/api/artiste/content-types/artiste/lifecycles.js b/src/api/artiste/content-types/artiste/lifecycles.js index e32d506..9c1abf0 100644 --- a/src/api/artiste/content-types/artiste/lifecycles.js +++ b/src/api/artiste/content-types/artiste/lifecycles.js @@ -5,7 +5,7 @@ * to customize this model */ -const { ValidationError } = require("@strapi/utils").errors +const { ApplicationError } = require("@strapi/utils").errors const slugify = require('slugify') const jwennTeksEpiId = async data => { @@ -20,7 +20,7 @@ const jwennAwtisEpiId = async id => { const validateArtiste = alias => { if (!alias || alias.trim().length === 0) { - throw new ValidationError('Champ obligatoire. Veuillez choisir un alias.'); + throw new ApplicationError('Champ obligatoire. Veuillez choisir un alias.'); } } diff --git a/src/api/commentaire/content-types/commentaire/lifecycles.js b/src/api/commentaire/content-types/commentaire/lifecycles.js index 73dab34..61f9ca4 100644 --- a/src/api/commentaire/content-types/commentaire/lifecycles.js +++ b/src/api/commentaire/content-types/commentaire/lifecycles.js @@ -1,6 +1,6 @@ 'use strict'; -const { ValidationError, NotFoundError } = require("@strapi/utils").errors +const { ApplicationError, NotFoundError } = require("@strapi/utils").errors const jwennUserEpiId = async userId => { if (!userId) { @@ -28,15 +28,15 @@ const jwennParoleEpiId = async paroleId => { const validateCommentaire = data => { if (!data.contenu && !data.datePublication) { - throw new ValidationError('Mauvaise requête, contenu et datePublication sont obligatoires') + throw new ApplicationError('Mauvaise requête, contenu et datePublication sont obligatoires') } if (!data.contenu || data.contenu.trim().length === 0) { - throw new ValidationError('Champ obligatoire. Veuillez renseigner le contenu du commentaire.') + throw new ApplicationError('Champ obligatoire. Veuillez renseigner le contenu du commentaire.') } if (data.contenu.trim().length > 500) { - throw new ValidationError('Le commentaire doit contenir 500 caractères maximum.') + throw new ApplicationError('Le commentaire doit contenir 500 caractères maximum.') } } diff --git a/src/api/commentaire/controllers/commentaire.js b/src/api/commentaire/controllers/commentaire.js index a19ef6f..d5baf83 100644 --- a/src/api/commentaire/controllers/commentaire.js +++ b/src/api/commentaire/controllers/commentaire.js @@ -1,7 +1,7 @@ 'use strict'; const { createCoreController } = require('@strapi/strapi').factories; -const { ValidationError, NotFoundError, UnauthorizedError } = require("@strapi/utils").errors +const { ApplicationError, NotFoundError, UnauthorizedError } = require("@strapi/utils").errors module.exports = createCoreController('api::commentaire.commentaire', ({strapi}) => ({ async create(ctx) { @@ -28,7 +28,7 @@ module.exports = createCoreController('api::commentaire.commentaire', ({strapi}) } if (user.id !== data.user.id || user.username !== data.user.username || user.email !== data.user.email) { - throw new ValidationError('Informations non valides.') + throw new ApplicationError('Informations non valides.') } data.user = user.id diff --git a/src/api/parole/content-types/parole/lifecycles.js b/src/api/parole/content-types/parole/lifecycles.js index b5889ea..49f8a97 100644 --- a/src/api/parole/content-types/parole/lifecycles.js +++ b/src/api/parole/content-types/parole/lifecycles.js @@ -2,7 +2,9 @@ const slugify = require('slugify') const axios = require('axios') -const { ValidationError } = require("@strapi/utils").errors + +const utils = require('@strapi/utils') +const { ApplicationError } = utils.errors const TELEGRAM_API_URL = 'https://api.telegram.org' const TELEGRAM_CHAN_ID = process.env.TELEGRAM_CHAN_ID || null @@ -28,7 +30,7 @@ const isSlugExists = async existingSlug => { const jwennAwtisEpiId = async artistesIds => { console.log('artistesIds', artistesIds) if (artistesIds.length === 0) { - throw new ValidationError('Champ obligatoire. Veuillez choisir au moins un artiste.'); + throw new ApplicationError('Champ obligatoire. Veuillez choisir au moins un artiste.'); } const artistes = await strapi.db.query('api::artiste.artiste').findMany({ @@ -53,7 +55,7 @@ const jwennUserEpiId = async userId => { }) if (!user) { - throw new ValidationError('Utilisateur introuvable.') + throw new ApplicationError('Utilisateur introuvable.') } return user @@ -128,7 +130,7 @@ module.exports = { const getSlugExistance = await isSlugExists(data.slug) if (getSlugExistance) { - throw new ValidationError('Un morceau du même artiste existe déjà.') + throw new ApplicationError('Un morceau du même artiste existe déjà.') } }, beforeUpdate: async event => { diff --git a/src/api/parole/services/parole.js b/src/api/parole/services/parole.js index a2fda82..190b4a1 100644 --- a/src/api/parole/services/parole.js +++ b/src/api/parole/services/parole.js @@ -4,7 +4,7 @@ const axios = require('axios') const Diff = require('diff') const { createCoreService } = require('@strapi/strapi').factories; -const { ValidationError } = require("@strapi/utils").errors +const { ApplicationError } = require("@strapi/utils").errors class Translator { constructor() { @@ -56,15 +56,15 @@ module.exports = createCoreService('api::parole.parole', ({strapi}) => ({ }, validateParoles(titre, transcription) { if (!titre || titre.trim().length === 0) { - throw new ValidationError('Champ obligatoire. Veuillez choisir un titre.'); + throw new ApplicationError('Champ obligatoire. Veuillez choisir un titre.'); } if (!transcription || transcription.trim().length === 0) { - throw new ValidationError('Champ obligatoire. Veuillez renseigner la transcription.') + throw new ApplicationError('Champ obligatoire. Veuillez renseigner la transcription.') } if (transcription.trim().length < 10) { - throw new ValidationError('La transcription doit contenir au moins 10 caractères.') + throw new ApplicationError('La transcription doit contenir au moins 10 caractères.') } }, parolesDiff(titre = '', oldString, newString) {