From f7a09c02aef4e946df08890c997b7b795003217b Mon Sep 17 00:00:00 2001 From: Alexis <35.alexis.pele@gmail.com> Date: Mon, 28 Dec 2020 12:23:54 +0100 Subject: [PATCH] - Removed HttpError and refactored old code using it --- api/repositories/ingredient-repository.js | 82 ++++++++---- api/repositories/meta-school-repository.js | 17 +-- api/repositories/school-repository.js | 94 +++++++++----- api/repositories/spell-repository.js | 138 ++++++++++++++------- api/repositories/user-repository.js | 52 ++++---- api/repositories/variable-repository.js | 79 ++++++++---- api/validations/Errors.js | 13 -- 7 files changed, 311 insertions(+), 164 deletions(-) delete mode 100644 api/validations/Errors.js diff --git a/api/repositories/ingredient-repository.js b/api/repositories/ingredient-repository.js index e91aeb9..247e9bb 100644 --- a/api/repositories/ingredient-repository.js +++ b/api/repositories/ingredient-repository.js @@ -13,9 +13,6 @@ v.addSchema(IngredientValidation, "/IngredientValidation") const isXSSAttempt = require('../functions').isXSSAttempt const isEmptyObject = require('../functions').isEmptyObject -// Error handling -const { HttpError } = require('../validations/Errors') - class IngredientRepository { constructor() { @@ -23,22 +20,25 @@ class IngredientRepository { getAll() { return new Promise((resolve, reject) => { - model.forge() + new model() .fetchAll({ withRelated: ['spells'] }) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get ingredients")) - }) + reject({ + "message": "Il n'existe aucun ingrédient disponible.", + "code": 404, + }); + }) }) } getOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['spells']}) .then(v => { @@ -46,14 +46,17 @@ class IngredientRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get ingredient")) + reject({ + "message": "L'ingrédient en question n'a pas pu être trouvé.", + "code": 404, + }); }) }) } getSpellsFromOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['spells', 'spells.schools', 'spells.variables', 'spells.ingredients', 'spells.schools.meta_schools']}) .then(v => { @@ -61,7 +64,10 @@ class IngredientRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get ingredient")) + reject({ + "message": "Les sortilèges liés à cet ingrédient n'ont pas pu être récupérés.", + "code": 404, + }); }) }) } @@ -70,14 +76,23 @@ class IngredientRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(igr)) { - reject(new HttpError(403, "Error: Ingredient cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(igr, IngredientValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors)) + reject({ + "message": `Le modèle d'ingrédient n'est pas respecté : ${v.validate(s, IngredientValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(igr.description)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { bookshelf.transaction(t => { - return model.forge({ + return new model({ 'name': igr.name, 'description': igr.description, }).save(null, { @@ -95,7 +110,10 @@ class IngredientRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Insert Ingredient error")) + reject({ + "message": "Une erreur d'insertion s'est produite.", + "code": 500, + }); }) } }) @@ -105,13 +123,22 @@ class IngredientRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(igr)) { - reject(new HttpError(403, "Error: Ingredient cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(igr, IngredientValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(igr, IngredientValidation).errors)) + reject({ + "message": `Le modèle d'ingrédient n'est pas respecté : ${v.validate(s, IngredientValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(igr.description)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { - model.forge({id: id}) + new model({id: id}) .fetch({require: true, withRelated: ['spells']}) .then(v => { bookshelf.transaction(t => { @@ -135,12 +162,18 @@ class IngredientRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Update Ingredient error")) + reject({ + "message": "Une erreur d'insertion s'est produite.", + "code": 500, + }); }) }) .catch(err => { console.log(err) - reject(new HttpError(404, "Couldn't get ingredient")) + reject({ + "message": "L'ingrédient en question n'a pas été trouvé.", + "code": 404, + }); }) } }) @@ -148,7 +181,7 @@ class IngredientRepository { deleteOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({require: true, withRelated: ['spells']}) .then(v => { @@ -162,7 +195,10 @@ class IngredientRepository { }) .catch(err => { console.log(err) - reject(new HttpError(404, "Couldn't get ingredient")) + reject({ + "message": "L'ingrédient en question n'a pas été trouvé.", + "code": 404, + }); }) }) } diff --git a/api/repositories/meta-school-repository.js b/api/repositories/meta-school-repository.js index a259622..dad1439 100644 --- a/api/repositories/meta-school-repository.js +++ b/api/repositories/meta-school-repository.js @@ -12,9 +12,6 @@ v.addSchema(MetaSchoolValidation, "/MetaSchoolValidation") // Validations const regexXSS = RegExp(/<[^>]*script/) -// Error handling -const { HttpError } = require('../validations/Errors') - class MetaSchoolRepository { constructor() { @@ -22,21 +19,24 @@ class MetaSchoolRepository { getAll() { return new Promise((resolve, reject) => { - model.forge() + new model() .fetchAll({ withRelated: ['schools'] }) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get meta schools")) + reject({ + "message": "Il n'existe aucune grande école disponible.", + "code": 404, + }); }) }) } getOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['schools']}) .then(v => { @@ -44,7 +44,10 @@ class MetaSchoolRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get meta school")) + reject({ + "message": "La grande école en question n'a pas pu être trouvée.", + "code": 404, + }); }) }) } diff --git a/api/repositories/school-repository.js b/api/repositories/school-repository.js index 0aef22b..274c4a3 100644 --- a/api/repositories/school-repository.js +++ b/api/repositories/school-repository.js @@ -13,9 +13,6 @@ v.addSchema(SchoolValidation, "/SchoolValidation") const isXSSAttempt = require('../functions').isXSSAttempt const isEmptyObject = require('../functions').isEmptyObject -// Error handling -const { HttpError } = require('../validations/Errors') - class SchoolRepository { constructor() { @@ -23,44 +20,53 @@ class SchoolRepository { getAll() { return new Promise((resolve, reject) => { - model.forge() + new model() .fetchAll({ withRelated: ['meta_schools'] }) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get schools")) + console.log(err); + reject({ + "message": "Il n'existe aucune école disponible.", + "code": 404, + }); }) }) } getOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['meta_schools']}) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get school")) + console.log(err); + reject({ + "message": "L'école en question n'a pas pu être trouvée.", + "code": 404, + }); }) }) } getSpellsFromOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['spells', 'spells.schools', 'spells.variables', 'spells.ingredients', 'spells.schools.meta_schools']}) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get spells from school")) + console.log(err); + reject({ + "message": "Les sortilèges de cette école n'ont pas pu être récupérés.", + "code": 404, + }); }) }) } @@ -69,14 +75,23 @@ class SchoolRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(s)) { - reject(new HttpError(403, "Error: School cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(s, SchoolValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors)) + reject({ + "message": `Le modèle d'école n'est pas respecté : ${v.validate(s, SchoolValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { bookshelf.transaction(t => { - return model.forge({ + return new model({ 'name': s.name, 'description': s.description, 'meta_school_id': s.meta_school_id, @@ -88,14 +103,17 @@ class SchoolRepository { }) }) .then(v => { - return v.load(['meta_schools']) + return v.load(['meta_schools']); }) .then(v => { - resolve(this.getOne(v.id)) + resolve(this.getOne(v.id)); }) .catch(err => { console.log(err) - reject(new HttpError(500, "Insert School error")) + reject({ + "message": "Une erreur d'insertion s'est produite.", + "code": 500, + }) }) } }) @@ -105,13 +123,22 @@ class SchoolRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(s)) { - reject(new HttpError(403, "Error: School cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(s, SchoolValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SchoolValidation).errors)) + reject({ + "message": `Le modèle d'école n'est pas respecté : ${v.validate(s, SchoolValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { - model.forge({id: id}) + new model({id: id}) .fetch({require: true, withRelated: ['meta_schools']}) .then(v => { bookshelf.transaction(t => { @@ -136,12 +163,18 @@ class SchoolRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Update School error")) + reject({ + "message": "Une erreur d'insertion s'est produite.", + "code": 500, + }) }) }) .catch(err => { - console.log(err) - reject(new HttpError(404, "Couldn't get school")) + console.log(err); + reject({ + "message": "L'école en question n'a pas été trouvée.", + "code": 404, + }); }) } }) @@ -149,7 +182,7 @@ class SchoolRepository { deleteOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({require: true, withRelated: ['spells', 'meta_schools']}) .then(v => { @@ -162,8 +195,11 @@ class SchoolRepository { }) }) .catch(err => { - console.log(err) - reject(new HttpError(404, "Couldn't get school")) + console.log(err); + reject({ + "message": "L'école en question n'a pas été trouvée.", + "code": 404, + }); }) }) } diff --git a/api/repositories/spell-repository.js b/api/repositories/spell-repository.js index da8b5d2..6e45138 100644 --- a/api/repositories/spell-repository.js +++ b/api/repositories/spell-repository.js @@ -13,9 +13,6 @@ v.addSchema(SpellValidation, "/SpellValidation") const isXSSAttempt = require('../functions').isXSSAttempt const isEmptyObject = require('../functions').isEmptyObject -// Error handling -const { HttpError } = require('../validations/Errors') - class SpellRepository { constructor() { @@ -24,7 +21,7 @@ class SpellRepository { getAll(name, description, level, charge, cost, ritual) { return new Promise((resolve, reject) => { - let query = model.forge() + let query = new model(); if (name) { query.where('name', 'like', `%${name}%`) } if (description) { query.where('description', 'like', `%${description}%`) } @@ -38,8 +35,11 @@ class SpellRepository { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get spells")) + console.log(err); + reject({ + "message": "Il n'existe aucun sortilège disponible.", + "code": 404, + }); }) }) } @@ -47,8 +47,7 @@ class SpellRepository { getAllPublic(name, description, level, charge, cost, ritual) { return new Promise((resolve, reject) => { - let query = model.forge() - .where({ 'public' : 1 }) + let query = new model().where({ 'public' : 1 }) if (name) { query.where('name', 'like', `%${name}%`) } if (description) { query.where('description', 'like', `%${description}%`) } @@ -59,18 +58,21 @@ class SpellRepository { query.fetchAll({ withRelated: ['schools.meta_schools', 'variables', 'ingredients'] }) .then(v => { - resolve(v.toJSON({ omitPivot: true })) + resolve(v.toJSON({ omitPivot: true })); }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get public spells")) + console.log(err); + reject({ + "message": "Il n'existe aucun sortilège disponible.", + "code": 404, + }); }) }) } getPage(page) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'public' : 1 }) .fetchPage({ pageSize: 20, @@ -81,23 +83,29 @@ class SpellRepository { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get public spells")) + console.log(err); + reject({ + "message": "La page de sortilèges n'a pas pu être chargée", + "code": 404, + }); }) }) } getOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['schools.meta_schools', 'variables', 'ingredients']}) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Couldn't get spell")) + console.log(err); + reject({ + "message": "Le sortilège en question n'a pas été trouvé.", + "code": 404, + }); }) }) } @@ -106,14 +114,23 @@ class SpellRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(s)) { - reject(new HttpError(403, "Error: Spell cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(s, SpellValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors)) + reject({ + "message": `Le modèle de sortilège n'est pas respecté : ${v.validate(s, SpellValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { bookshelf.transaction(t => { - return model.forge({ + return new model({ 'name': s.name, 'description': s.description, 'level': s.level, @@ -145,7 +162,11 @@ class SpellRepository { }); }) .catch(err => { - throw err + console.log(err); + reject({ + "message": "Un attributs du sortilège a provoqué une erreur d'insertion.", + "code": 500, + }); }) }) .then(v => { @@ -155,8 +176,11 @@ class SpellRepository { resolve(this.getOne(v.id)) }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Insert Spell error")) + console.log(err); + reject({ + "message": "Le sortilège n'a pas pu être ajouté.", + "code": 500, + }); }) } }) @@ -166,13 +190,22 @@ class SpellRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(s)) { - reject(new HttpError(403, "Error: Spell cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(s, SpellValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(s, SpellValidation).errors)) + reject({ + "message": `Le modèle de sortilège n'est pas respecté : ${v.validate(s, SpellValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(s.name) || isXSSAttempt(s.description) || isXSSAttempt(s.cost)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { - model.forge({id: id}) + new model({id: id}) .fetch({require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients']}) .then(v => { bookshelf.transaction(t => { @@ -193,14 +226,12 @@ class SpellRepository { let schools = spell.related('school'); return spell.schools().detach(schools, { transacting: t}); } - return }) .tap(spell => { if (s.variables) { let variables = spell.related('variable'); return spell.variables().detach(variables, { transacting: t}); } - return }) .tap(spell => { if (s.ingredients) { @@ -230,24 +261,33 @@ class SpellRepository { }); }) .catch(err => { - console.log(err) - throw err + console.log(err); + reject({ + "message": "Un attributs du sortilège a provoqué une erreur d'insertion.", + "code": 500, + }) }) }) .then(v => { - return v.load(['schools.meta_schools', 'variables', 'ingredients']) + return v.load(['schools.meta_schools', 'variables', 'ingredients']); }) .then(v => { - resolve(this.getOne(v.id)) + resolve(this.getOne(v.id)); }) .catch(err => { - console.log(err) - reject(new HttpError(500, "Update Spell error")) + console.log(err); + reject({ + "message": "Une erreur de chargement est survenue.", + "code": 500, + }); }) }) .catch(err => { - console.log(err) - reject(new HttpError(404, "Couldn't get spell")) + console.log(err); + reject({ + "message": "Le sortilège en question n'a pas été trouvé.", + "code": 404, + }); }) } }) @@ -255,23 +295,27 @@ class SpellRepository { deleteOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({require: true, withRelated: ['schools.meta_schools', 'variables', 'ingredients']}) .then(v => { - v.schools().detach() - v.variables().detach() - v.ingredients().detach() - v.destroy() + v.schools().detach(); + v.variables().detach(); + v.ingredients().detach(); + v.destroy(); }) .then(() => { resolve({ - 'message': 'Spell with ID ' + id + ' successfully deleted !' - }) + "message": `Le sortilège #${id} a été supprimé avec succès.`, + "code": 200, + }); }) .catch(err => { - console.log(err) - reject(new HttpError(404, "Couldn't get spell")) + console.log(err); + reject({ + "message": "Le sortilège en question n'a pas été trouvé.", + "code": 404, + }); }) }) } diff --git a/api/repositories/user-repository.js b/api/repositories/user-repository.js index dd5d3a3..ff8c282 100644 --- a/api/repositories/user-repository.js +++ b/api/repositories/user-repository.js @@ -27,50 +27,50 @@ class UserRepository { getAll() { return new Promise((resolve, reject) => { - model.forge() + new model() .fetchAll() .then(v => { - resolve(v.toJSON({ omitPivot: true })) + resolve(v.toJSON({ omitPivot: true })); }) .catch(() => { reject({ "message": "Erreur de base de données, les utilisateurs n'ont pas pu être récupérés.", - "code": 500 - }) + "code": 500, + }); }) }) } getOneByUUID(uuid, full) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'uuid' : uuid }) .fetch() .then(v => { - resolve(v.toJSON({ omitPivot: true, visibility: !full })) + resolve(v.toJSON({ omitPivot: true, visibility: !full })); }) - .catch(err => { + .catch(() => { reject({ "message": "L'utilisateur avec cet UUID n'a pas été trouvé.", - "code": 404 - }) + "code": 404, + }); }) }) } getOneByEmail(mail, full) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'mail': mail }) .fetch() .then(v => { - resolve(v.toJSON({ omitPivot: true, visibility: !full })) + resolve(v.toJSON({ omitPivot: true, visibility: !full })); }) .catch(() => { reject({ "message": "L'utilisateur avec cet email n'a pas été trouvé.", - "code": 404 - }) + "code": 404, + }); }) }) } @@ -81,17 +81,17 @@ class UserRepository { if (isEmptyObject(u)) { reject({ "message": "Le corps de requête ne peut être vide.", - "code": 403 + "code": 403, }) } else if (!v.validate(u, UserValidation).valid) { reject({ "message": "Structure de la requête invalide - " + v.validate(u, UserValidation).errors, - "code": 403 + "code": 403, }) } else if (isXSSAttempt(u.name) || isXSSAttempt(u.password) || isXSSAttempt(u.mail)) { reject({ "message": "Essai d'injection détecté, avortement de la requête.", - "code": 403 + "code": 403, }) } else { let hash = await bcrypt.hash(u.password, 10) @@ -102,7 +102,7 @@ class UserRepository { this.checkIfEmailAvailable(u.mail) .then(() => { bookshelf.transaction(t => { - return model.forge({ + return new model({ 'uuid': uuid, 'name': u.name, 'mail': u.mail, @@ -113,7 +113,11 @@ class UserRepository { transacting: t }) .catch(err => { - throw err + console.log(err); + reject({ + "message": "Un attributs de l'utilisateur a provoqué une erreur d'insertion.", + "code": 500, + }); }) }) .then(() => { @@ -153,7 +157,7 @@ class UserRepository { verifyUser(token) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'verification_token' : token }) .fetch() .then(v => { @@ -176,7 +180,7 @@ class UserRepository { .catch(() => { reject({ "message": "Le lien de vérification ne semble pas correct.", - "code": 404 + "code": 404, }) }) }); @@ -196,12 +200,12 @@ class UserRepository { if (fetchedUser.banned) { reject({ "message": `L'utilisateur #${fetchedUser.name} a été banni, la connexion est impossible.`, - "code": 403 + "code": 403, }) } else if (!fetchedUser.verified) { reject({ "message": `L'utilisateur #${fetchedUser.name} n'as pas été vérifié, le compte doit être activé avant la connexion.`, - "code": 401 + "code": 401, }) } else { resolve({ @@ -238,13 +242,13 @@ class UserRepository { .then(() => { reject({ "message": "Cet email est déjà lié à un compte.", - "code": 409 + "code": 409, }) }) .catch(() => { resolve({ "message": "Cet email est disponible.", - "code": 200 + "code": 200, }) }) }) diff --git a/api/repositories/variable-repository.js b/api/repositories/variable-repository.js index 2fc2a31..314941b 100644 --- a/api/repositories/variable-repository.js +++ b/api/repositories/variable-repository.js @@ -13,8 +13,6 @@ v.addSchema(VariableValidation, "/VariableValidation") const isXSSAttempt = require('../functions').isXSSAttempt const isEmptyObject = require('../functions').isEmptyObject -// Error handling -const { HttpError } = require('../validations/Errors') class VariableRepository { constructor() { @@ -22,14 +20,17 @@ class VariableRepository { getAll() { return new Promise((resolve, reject) => { - model.forge() + new model() .fetchAll({ withRelated: ['spells'] }) .then(v => { resolve(v.toJSON({ omitPivot: true })) }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get variables")) + reject({ + "message": "Il n'existe aucune variable disponible.", + "code": 404, + }); }) }) } @@ -37,7 +38,7 @@ class VariableRepository { getOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['spells']}) .then(v => { @@ -45,14 +46,17 @@ class VariableRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get variable")) + reject({ + "message": "La variable en question n'a pas pu être trouvée.", + "code": 404, + }); }) }) } getSpellsFromOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({ withRelated: ['spells', 'spells.schools', 'spells.variables', 'spells.ingredients', 'spells.schools.meta_schools']}) .then(v => { @@ -60,7 +64,10 @@ class VariableRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Couldn't get variable")) + reject({ + "message": "Les sortilèges liés à cette variable n'ont pas pu être récupérés.", + "code": 404, + }); }) }) } @@ -69,14 +76,23 @@ class VariableRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(vr)) { - reject(new HttpError(403, "Error: Variable cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(vr, VariableValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors)) + reject({ + "message": `Le modèle de variable n'est pas respecté : ${v.validate(s, VariableValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(vr.description)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { bookshelf.transaction(t => { - return model.forge({ + return new model({ 'description': vr.description, }).save(null, { transacting: t @@ -93,7 +109,10 @@ class VariableRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Insert Variable error")) + reject({ + "message": "Une erreur d'insertion s'est produite.", + "code": 500, + }); }) } }) @@ -103,13 +122,22 @@ class VariableRepository { return new Promise((resolve, reject) => { // Checks if body exists and if the model fits, and throws errors if it doesn't if (isEmptyObject(vr)) { - reject(new HttpError(403, "Error: Variable cannot be nothing !")) + reject({ + "message": "Le corps de la requête ne peut pas être vide.", + "code": 403, + }); } else if (!v.validate(vr, VariableValidation).valid) { - reject(new HttpError(403, "Error: Schema is not valid - " + v.validate(vr, VariableValidation).errors)) + reject({ + "message": `Le modèle de variable n'est pas respecté : ${v.validate(s, VariableValidation).errors}`, + "code": 403, + }); } else if (isXSSAttempt(vr.description)) { - reject(new HttpError(403, 'Injection attempt detected, aborting the request.')) + reject({ + "message": "Tentative d'injection XSS détectée, requête refusée.", + "code": 403, + }); } else { - model.forge({id: id}) + new model({id: id}) .fetch({require: true, withRelated: ['spells']}) .then(v => { bookshelf.transaction(t => { @@ -132,12 +160,18 @@ class VariableRepository { }) .catch(err => { console.log(err) - reject(new HttpError(500, "Update Variable error")) + reject({ + "message": "Une erreur d'insertion s'est produite.", + "code": 500, + }); }) }) .catch(err => { console.log(err) - reject(new HttpError(404, "Couldn't get variable")) + reject({ + "message": "La variable en question n'a pas été trouvée.", + "code": 404, + }); }) } }) @@ -145,7 +179,7 @@ class VariableRepository { deleteOne(id) { return new Promise((resolve, reject) => { - model.forge() + new model() .where({ 'id' : id }) .fetch({require: true, withRelated: ['spells']}) .then(v => { @@ -159,7 +193,10 @@ class VariableRepository { }) .catch(err => { console.log(err) - reject(new HttpError(404, "Couldn't get variable")) + reject({ + "message": "La variable en question n'a pas été trouvée.", + "code": 404, + }); }) }) } diff --git a/api/validations/Errors.js b/api/validations/Errors.js deleted file mode 100644 index f7e1181..0000000 --- a/api/validations/Errors.js +++ /dev/null @@ -1,13 +0,0 @@ -// Http error w/ http code -class HttpError extends Error { - constructor(code, message) { - super(message) - this.name = "HttpError" - this.code = code - Error.captureStackTrace(this, this.constructor); - } -} - -module.exports = { - HttpError -} \ No newline at end of file