Replace ValidationError by ApplicationError in services & lifecycles

This commit is contained in:
2023-04-02 12:25:52 +04:00
parent a65b3f04e8
commit d61ef75869
5 changed files with 18 additions and 16 deletions
@@ -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.');
}
}
@@ -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.')
}
}
@@ -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
@@ -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 => {
+4 -4
View File
@@ -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) {