backend: Error handling middleware

This commit is contained in:
Divlo 2020-03-19 22:59:06 +01:00
parent c324676e89
commit a2b5a02f6b
13 changed files with 71 additions and 78 deletions

View File

@ -6,10 +6,9 @@ const cors = require('cors');
const morgan = require('morgan');
/* Files Imports & Variables */
const { PORT } = require('./assets/config/config');
const { serverError } = require('./assets/config/errors');
const sendResponse = require('./assets/utils/sendResponse');
const app = express();
const { PORT } = require('./assets/config/config');
const errorHandling = require('./assets/utils/errorHandling');
const app = express();
/* Middlewares */
app.use(helmet());
@ -21,8 +20,12 @@ app.use(express.json());
app.use('/functions', require('./routes/functions'));
/* Errors Handling */
app.use((_req, res, _next) => sendResponse(res, { result: "La route n'existe pas!", httpStatus: 404 })); // 404
app.use((error, _req, res, _next) => { console.log(error); return sendResponse(res, serverError); }); // 500
app.use((_req, _res, next) => errorHandling(next, { statusCode: 404, message: "La route n'existe pas!" })); // 404
app.use((error, _req, res, _next) => {
console.log(error);
const { statusCode, message } = error;
return res.status(statusCode || 500).json({ message });
});
/* Server */
app.listen(PORT, () => console.log('\x1b[36m%s\x1b[0m', `Started on port ${PORT}.`));

View File

@ -1,17 +1,12 @@
const errors = {
generalError: {
result: "Vous n'avez pas rentré de valeur valide.",
httpStatus: 400
},
serverError: {
result: "Le serveur n'a pas pu traiter votre requête.",
httpStatus: 500
message: "Vous n'avez pas rentré de valeur valide.",
statusCode: 400
},
requiredFields: {
result: "Vous devez remplir tous les champs...",
httpStatus: 400
message: "Vous devez remplir tous les champs...",
statusCode: 400
}
};

View File

@ -1,4 +1,4 @@
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields } = require('../../config/errors');
const formatNumberResult = require('../secondary/formatNumberResult');
@ -28,19 +28,19 @@ function armstrongNumber(number) {
}
/* OUTPUTS */
exports.armstrongNumberOutput = (res, argsObject) => {
exports.armstrongNumberOutput = ({ res, next }, argsObject) => {
let { number } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(number)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce n'est pas un nombre
number = parseInt(number);
if (isNaN(number) || number <= 0) {
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
}
return sendResponse(res, { result: armstrongNumber(number) }, true);
return res.status(200).json(armstrongNumber(number));
}

View File

@ -1,4 +1,4 @@
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const moment = require('moment');
const { requiredFields } = require('../../config/errors');
@ -20,7 +20,7 @@ function calculateAge(currentDate, { birthDateDay, birthDateMonth, birthDateYear
}
/* OUTPUTS */
exports.calculateAgeOutput = (res, argsObject) => {
exports.calculateAgeOutput = ({ res, next }, argsObject) => {
let { birthDateDay, birthDateMonth, birthDateYear } = argsObject;
birthDateDay = parseInt(birthDateDay);
birthDateMonth = parseInt(birthDateMonth);
@ -28,16 +28,15 @@ exports.calculateAgeOutput = (res, argsObject) => {
// S'il n'y a pas les champs obligatoire
if (!(birthDateDay && birthDateMonth && birthDateYear)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce n'est pas une date valide
const currentDate = new Date();
const birthDate = new Date(birthDateYear, birthDateMonth - 1, birthDateDay);
if (!(currentDate > birthDate)) {
return sendResponse(res, { result: "Veuillez rentré une date valide...", httpStatus: 400 });
return errorHandling(next, { message: "Veuillez rentré une date valide...", statusCode: 400 });
}
const result = calculateAge(currentDate, { birthDateYear, birthDateMonth, birthDateDay });
return sendResponse(res, { result }, true);
return res.status(200).json(calculateAge(currentDate, { birthDateYear, birthDateMonth, birthDateDay }));
}

View File

@ -1,20 +1,20 @@
const axios = require('axios');
const sendResponse = require('../../utils/sendResponse');
const axios = require('axios');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields } = require('../../config/errors');
/* OUTPUTS */
exports.convertCurrencyOutput = (res, argsObject) => {
exports.convertCurrencyOutput = ({ res, next }, argsObject) => {
let { number, baseCurrency, finalCurrency } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(number && baseCurrency && finalCurrency)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce n'est pas un nombre
number = parseFloat(number);
if (isNaN(number)) {
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
}
axios.get(`https://api.exchangeratesapi.io/latest?base=${baseCurrency}`)
@ -25,7 +25,7 @@ exports.convertCurrencyOutput = (res, argsObject) => {
const year = dateObject.getFullYear();
const day = ('0'+(dateObject.getDate())).slice(-2);
const month = ('0'+(dateObject.getMonth()+1)).slice(-2);
return sendResponse(res, { result: { date: `${day}/${month}/${year}`, result } }, true);
return res.status(200).json({ date: `${day}/${month}/${year}`, result });
})
.catch(() => sendResponse(res, { result: "La devise n'existe pas.", httpStatus: 404 }));
.catch(() => errorHandling(next, { message: "La devise n'existe pas.", statusCode: 404 }));
}

View File

@ -1,4 +1,4 @@
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields, generalError } = require('../../config/errors');
const correspondancesDistance = ["pm", null, null, "nm", null, null, "µm", null, null, "mm", "cm", "dm", "m", "dam", "hm", "km", null, null, "Mm", null, null, "Gm", null, null, "Tm"];
@ -27,18 +27,18 @@ function convertDistance(firstValue, unitFirstValue, unitFinalValue) {
}
/* OUTPUTS */
exports.convertDistanceOutput = (res, argsObject) => {
exports.convertDistanceOutput = ({ res, next }, argsObject) => {
let { number, numberUnit, finalUnit } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(number && numberUnit && finalUnit)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce n'est pas un nombre
number = parseInt(number);
if (isNaN(number)) {
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
}
const result = convertDistance(number, numberUnit, finalUnit);
@ -46,5 +46,5 @@ exports.convertDistanceOutput = (res, argsObject) => {
return sendResponse(res, generalError);
}
return sendResponse(res, { result }, true);
return res.status(200).json(result);
}

View File

@ -1,4 +1,4 @@
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields, generalError } = require('../../config/errors');
/* Variable pour convertRomanArabicNumbers */
@ -67,12 +67,12 @@ function convertRomanToArabic(str) {
}
/* OUTPUTS */
exports.convertRomanToArabicOutput = (res, argsObject) => {
exports.convertRomanToArabicOutput = ({ res, next }, argsObject) => {
let { romanNumber } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(romanNumber)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Formate le paramètre
@ -80,30 +80,30 @@ exports.convertRomanToArabicOutput = (res, argsObject) => {
romanNumber = romanNumber.toUpperCase();
}
catch {
return sendResponse(res, generalError);
return errorHandling(next, generalError);
}
const result = convertRomanToArabic(romanNumber);
if (result === 0) {
return sendResponse(res, generalError);
return errorHandling(next, generalError);
}
return sendResponse(res, { result }, true);
return res.status(200).json({ result });
}
exports.convertArabicToRomanOutput = (res, argsObject) => {
exports.convertArabicToRomanOutput = ({ res, next }, argsObject) => {
let { number } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(number)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce n'est pas un nombre
number = parseInt(number);
if (isNaN(number)) {
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
}
return sendResponse(res, { result: convertArabicToRoman(number) }, true);
return res.status(200).json({ result: convertArabicToRoman(number) });
}

View File

@ -1,4 +1,4 @@
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields, generalError } = require('../../config/errors');
/**
@ -29,24 +29,24 @@ function convertTemperature(degree, unit) {
}
/* OUTPUTS */
exports.convertTemperatureOutput = (res, argsObject) => {
exports.convertTemperatureOutput = ({ res, next }, argsObject) => {
let { degree, unit } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(degree && unit)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce n'est pas un nombre
degree = parseInt(degree);
if (isNaN(degree)) {
return sendResponse(res, { result: "Veuillez rentré un nombre valide.", httpStatus: 400 });
return errorHandling(next, { message: "Veuillez rentré un nombre valide.", statusCode: 400 });
}
const result = convertTemperature(degree, unit);
if (!result) {
return sendResponse(res, generalError);
return errorHandling(next, generalError);
}
return sendResponse(res, { result }, true);
return res.status(200).json({ result });
}

View File

@ -1,4 +1,4 @@
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields } = require('../../config/errors');
/**
@ -13,20 +13,20 @@ function randomNumber(min, max) {
}
/* OUTPUTS */
exports.randomNumberOutput = (res, argsObject) => {
exports.randomNumberOutput = ({ res, next }, argsObject) => {
let { min, max } = argsObject;
// S'il n'y a pas les champs obligatoire
if (!(min && max)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Si ce ne sont pas des nombres
min = parseInt(min);
max = parseInt(max);
if (isNaN(min) || isNaN(max)) {
return sendResponse(res, { result: "Les paramètres min et max doivent être des nombres...", httpStatus: 400 });
return errorHandling(next, { message: "Les paramètres min et max doivent être des nombres...", statusCode: 400 });
}
return sendResponse(res, { result: randomNumber(min, max) }, true);
return res.status(200).json({ result: randomNumber(min, max) });
}

View File

@ -1,6 +1,6 @@
const axios = require('axios');
const Queue = require('smart-request-balancer');
const sendResponse = require('../../utils/sendResponse');
const errorHandling = require('../../utils/errorHandling');
const { requiredFields } = require('../../config/errors');
const { WEATHER_API_KEY } = require('../../config/config');
@ -25,13 +25,13 @@ exports.weatherRequestOutput = async (res, argsObject) => {
// S'il n'y a pas les champs obligatoire
if (!(cityName)) {
return sendResponse(res, requiredFields);
return errorHandling(next, requiredFields);
}
// Récupère les données météo grâce à l'API : openweathermap.org. (→ avec limite de 50 requêtes par minute)
queue.request(() => {
axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&lang=fr&units=metric&appid=${WEATHER_API_KEY}`)
.then((response) => sendResponse(res, { result: response.data }, true))
.catch(() => sendResponse(res, { result: "La ville n'existe pas (dans l'API de openweathermap.org).", httpStatus: 404 }));
.then((response) => res.status(200).json(response.data))
.catch(() => errorHandling(next, { message: "La ville n'existe pas (dans l'API de openweathermap.org).", statusCode: 404 }));
}, 'everyone', 'weatherRequest');
}

View File

@ -0,0 +1,7 @@
function errorHandling(next, { statusCode, message }) {
const error = new Error(message);
error.statusCode = statusCode;
next(error);
}
module.exports = errorHandling;

View File

@ -1,11 +0,0 @@
/**
* @description Envoie la réponse au client
* @param {Response} res Objet réponse d'une réponse http/express
* @param {Object} object { httpStatus, customProperties{Object}, result }
* @param {Boolean} isSuccess (false par defaut)
*/
function sendResponse(res, object, isSuccess = false) {
res.status(object.httpStatus || 200).json({ isSuccess, ...object.customProperties, result: object.result });
}
module.exports = sendResponse;

View File

@ -1,10 +1,10 @@
const errorHandling = require('../assets/utils/errorHandling');
const functionToExecute = require('../assets/functions/functionObject');
const sendResponse = require('../assets/utils/sendResponse');
exports.executeFunctionName = (req, res, _next) => {
exports.executeFunctionName = (req, res, next) => {
const functionOutput = functionToExecute(req.params.functionName);
if (functionOutput !== undefined) {
return functionOutput(res, req.body);
return functionOutput({ res, next }, req.body);
}
return sendResponse(res, { result: "La fonction n'existe pas.", httpStatus: 404 });
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
}