backend: Error handling middleware
This commit is contained in:
parent
c324676e89
commit
a2b5a02f6b
@ -7,8 +7,7 @@ const morgan = require('morgan');
|
|||||||
|
|
||||||
/* Files Imports & Variables */
|
/* Files Imports & Variables */
|
||||||
const { PORT } = require('./assets/config/config');
|
const { PORT } = require('./assets/config/config');
|
||||||
const { serverError } = require('./assets/config/errors');
|
const errorHandling = require('./assets/utils/errorHandling');
|
||||||
const sendResponse = require('./assets/utils/sendResponse');
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|
||||||
/* Middlewares */
|
/* Middlewares */
|
||||||
@ -21,8 +20,12 @@ app.use(express.json());
|
|||||||
app.use('/functions', require('./routes/functions'));
|
app.use('/functions', require('./routes/functions'));
|
||||||
|
|
||||||
/* Errors Handling */
|
/* Errors Handling */
|
||||||
app.use((_req, res, _next) => sendResponse(res, { result: "La route n'existe pas!", httpStatus: 404 })); // 404
|
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); return sendResponse(res, serverError); }); // 500
|
app.use((error, _req, res, _next) => {
|
||||||
|
console.log(error);
|
||||||
|
const { statusCode, message } = error;
|
||||||
|
return res.status(statusCode || 500).json({ message });
|
||||||
|
});
|
||||||
|
|
||||||
/* Server */
|
/* Server */
|
||||||
app.listen(PORT, () => console.log('\x1b[36m%s\x1b[0m', `Started on port ${PORT}.`));
|
app.listen(PORT, () => console.log('\x1b[36m%s\x1b[0m', `Started on port ${PORT}.`));
|
@ -1,17 +1,12 @@
|
|||||||
const errors = {
|
const errors = {
|
||||||
generalError: {
|
generalError: {
|
||||||
result: "Vous n'avez pas rentré de valeur valide.",
|
message: "Vous n'avez pas rentré de valeur valide.",
|
||||||
httpStatus: 400
|
statusCode: 400
|
||||||
},
|
|
||||||
|
|
||||||
serverError: {
|
|
||||||
result: "Le serveur n'a pas pu traiter votre requête.",
|
|
||||||
httpStatus: 500
|
|
||||||
},
|
},
|
||||||
|
|
||||||
requiredFields: {
|
requiredFields: {
|
||||||
result: "Vous devez remplir tous les champs...",
|
message: "Vous devez remplir tous les champs...",
|
||||||
httpStatus: 400
|
statusCode: 400
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields } = require('../../config/errors');
|
const { requiredFields } = require('../../config/errors');
|
||||||
const formatNumberResult = require('../secondary/formatNumberResult');
|
const formatNumberResult = require('../secondary/formatNumberResult');
|
||||||
|
|
||||||
@ -28,19 +28,19 @@ function armstrongNumber(number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* OUTPUTS */
|
/* OUTPUTS */
|
||||||
exports.armstrongNumberOutput = (res, argsObject) => {
|
exports.armstrongNumberOutput = ({ res, next }, argsObject) => {
|
||||||
let { number } = argsObject;
|
let { number } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(number)) {
|
if (!(number)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce n'est pas un nombre
|
// Si ce n'est pas un nombre
|
||||||
number = parseInt(number);
|
number = parseInt(number);
|
||||||
if (isNaN(number) || number <= 0) {
|
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));
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const moment = require('moment');
|
const moment = require('moment');
|
||||||
const { requiredFields } = require('../../config/errors');
|
const { requiredFields } = require('../../config/errors');
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ function calculateAge(currentDate, { birthDateDay, birthDateMonth, birthDateYear
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* OUTPUTS */
|
/* OUTPUTS */
|
||||||
exports.calculateAgeOutput = (res, argsObject) => {
|
exports.calculateAgeOutput = ({ res, next }, argsObject) => {
|
||||||
let { birthDateDay, birthDateMonth, birthDateYear } = argsObject;
|
let { birthDateDay, birthDateMonth, birthDateYear } = argsObject;
|
||||||
birthDateDay = parseInt(birthDateDay);
|
birthDateDay = parseInt(birthDateDay);
|
||||||
birthDateMonth = parseInt(birthDateMonth);
|
birthDateMonth = parseInt(birthDateMonth);
|
||||||
@ -28,16 +28,15 @@ exports.calculateAgeOutput = (res, argsObject) => {
|
|||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(birthDateDay && birthDateMonth && birthDateYear)) {
|
if (!(birthDateDay && birthDateMonth && birthDateYear)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce n'est pas une date valide
|
// Si ce n'est pas une date valide
|
||||||
const currentDate = new Date();
|
const currentDate = new Date();
|
||||||
const birthDate = new Date(birthDateYear, birthDateMonth - 1, birthDateDay);
|
const birthDate = new Date(birthDateYear, birthDateMonth - 1, birthDateDay);
|
||||||
if (!(currentDate > birthDate)) {
|
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 res.status(200).json(calculateAge(currentDate, { birthDateYear, birthDateMonth, birthDateDay }));
|
||||||
return sendResponse(res, { result }, true);
|
|
||||||
}
|
}
|
@ -1,20 +1,20 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields } = require('../../config/errors');
|
const { requiredFields } = require('../../config/errors');
|
||||||
|
|
||||||
/* OUTPUTS */
|
/* OUTPUTS */
|
||||||
exports.convertCurrencyOutput = (res, argsObject) => {
|
exports.convertCurrencyOutput = ({ res, next }, argsObject) => {
|
||||||
let { number, baseCurrency, finalCurrency } = argsObject;
|
let { number, baseCurrency, finalCurrency } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(number && baseCurrency && finalCurrency)) {
|
if (!(number && baseCurrency && finalCurrency)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce n'est pas un nombre
|
// Si ce n'est pas un nombre
|
||||||
number = parseFloat(number);
|
number = parseFloat(number);
|
||||||
if (isNaN(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}`)
|
axios.get(`https://api.exchangeratesapi.io/latest?base=${baseCurrency}`)
|
||||||
@ -25,7 +25,7 @@ exports.convertCurrencyOutput = (res, argsObject) => {
|
|||||||
const year = dateObject.getFullYear();
|
const year = dateObject.getFullYear();
|
||||||
const day = ('0'+(dateObject.getDate())).slice(-2);
|
const day = ('0'+(dateObject.getDate())).slice(-2);
|
||||||
const month = ('0'+(dateObject.getMonth()+1)).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 }));
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields, generalError } = require('../../config/errors');
|
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"];
|
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 */
|
/* OUTPUTS */
|
||||||
exports.convertDistanceOutput = (res, argsObject) => {
|
exports.convertDistanceOutput = ({ res, next }, argsObject) => {
|
||||||
let { number, numberUnit, finalUnit } = argsObject;
|
let { number, numberUnit, finalUnit } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(number && numberUnit && finalUnit)) {
|
if (!(number && numberUnit && finalUnit)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce n'est pas un nombre
|
// Si ce n'est pas un nombre
|
||||||
number = parseInt(number);
|
number = parseInt(number);
|
||||||
if (isNaN(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);
|
const result = convertDistance(number, numberUnit, finalUnit);
|
||||||
@ -46,5 +46,5 @@ exports.convertDistanceOutput = (res, argsObject) => {
|
|||||||
return sendResponse(res, generalError);
|
return sendResponse(res, generalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sendResponse(res, { result }, true);
|
return res.status(200).json(result);
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields, generalError } = require('../../config/errors');
|
const { requiredFields, generalError } = require('../../config/errors');
|
||||||
|
|
||||||
/* Variable pour convertRomanArabicNumbers */
|
/* Variable pour convertRomanArabicNumbers */
|
||||||
@ -67,12 +67,12 @@ function convertRomanToArabic(str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* OUTPUTS */
|
/* OUTPUTS */
|
||||||
exports.convertRomanToArabicOutput = (res, argsObject) => {
|
exports.convertRomanToArabicOutput = ({ res, next }, argsObject) => {
|
||||||
let { romanNumber } = argsObject;
|
let { romanNumber } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(romanNumber)) {
|
if (!(romanNumber)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Formate le paramètre
|
// Formate le paramètre
|
||||||
@ -80,30 +80,30 @@ exports.convertRomanToArabicOutput = (res, argsObject) => {
|
|||||||
romanNumber = romanNumber.toUpperCase();
|
romanNumber = romanNumber.toUpperCase();
|
||||||
}
|
}
|
||||||
catch {
|
catch {
|
||||||
return sendResponse(res, generalError);
|
return errorHandling(next, generalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = convertRomanToArabic(romanNumber);
|
const result = convertRomanToArabic(romanNumber);
|
||||||
if (result === 0) {
|
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;
|
let { number } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(number)) {
|
if (!(number)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce n'est pas un nombre
|
// Si ce n'est pas un nombre
|
||||||
number = parseInt(number);
|
number = parseInt(number);
|
||||||
if (isNaN(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) });
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields, generalError } = require('../../config/errors');
|
const { requiredFields, generalError } = require('../../config/errors');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -29,24 +29,24 @@ function convertTemperature(degree, unit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* OUTPUTS */
|
/* OUTPUTS */
|
||||||
exports.convertTemperatureOutput = (res, argsObject) => {
|
exports.convertTemperatureOutput = ({ res, next }, argsObject) => {
|
||||||
let { degree, unit } = argsObject;
|
let { degree, unit } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(degree && unit)) {
|
if (!(degree && unit)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce n'est pas un nombre
|
// Si ce n'est pas un nombre
|
||||||
degree = parseInt(degree);
|
degree = parseInt(degree);
|
||||||
if (isNaN(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);
|
const result = convertTemperature(degree, unit);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
return sendResponse(res, generalError);
|
return errorHandling(next, generalError);
|
||||||
}
|
}
|
||||||
|
|
||||||
return sendResponse(res, { result }, true);
|
return res.status(200).json({ result });
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields } = require('../../config/errors');
|
const { requiredFields } = require('../../config/errors');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -13,20 +13,20 @@ function randomNumber(min, max) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* OUTPUTS */
|
/* OUTPUTS */
|
||||||
exports.randomNumberOutput = (res, argsObject) => {
|
exports.randomNumberOutput = ({ res, next }, argsObject) => {
|
||||||
let { min, max } = argsObject;
|
let { min, max } = argsObject;
|
||||||
|
|
||||||
// S'il n'y a pas les champs obligatoire
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(min && max)) {
|
if (!(min && max)) {
|
||||||
return sendResponse(res, requiredFields);
|
return errorHandling(next, requiredFields);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Si ce ne sont pas des nombres
|
// Si ce ne sont pas des nombres
|
||||||
min = parseInt(min);
|
min = parseInt(min);
|
||||||
max = parseInt(max);
|
max = parseInt(max);
|
||||||
if (isNaN(min) || isNaN(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) });
|
||||||
}
|
}
|
@ -1,6 +1,6 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const Queue = require('smart-request-balancer');
|
const Queue = require('smart-request-balancer');
|
||||||
const sendResponse = require('../../utils/sendResponse');
|
const errorHandling = require('../../utils/errorHandling');
|
||||||
const { requiredFields } = require('../../config/errors');
|
const { requiredFields } = require('../../config/errors');
|
||||||
const { WEATHER_API_KEY } = require('../../config/config');
|
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
|
// S'il n'y a pas les champs obligatoire
|
||||||
if (!(cityName)) {
|
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)
|
// 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(() => {
|
queue.request(() => {
|
||||||
axios.get(`https://api.openweathermap.org/data/2.5/weather?q=${cityName}&lang=fr&units=metric&appid=${WEATHER_API_KEY}`)
|
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))
|
.then((response) => res.status(200).json(response.data))
|
||||||
.catch(() => sendResponse(res, { result: "La ville n'existe pas (dans l'API de openweathermap.org).", httpStatus: 404 }));
|
.catch(() => errorHandling(next, { message: "La ville n'existe pas (dans l'API de openweathermap.org).", statusCode: 404 }));
|
||||||
}, 'everyone', 'weatherRequest');
|
}, 'everyone', 'weatherRequest');
|
||||||
}
|
}
|
7
backend/assets/utils/errorHandling.js
Normal file
7
backend/assets/utils/errorHandling.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
function errorHandling(next, { statusCode, message }) {
|
||||||
|
const error = new Error(message);
|
||||||
|
error.statusCode = statusCode;
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = errorHandling;
|
@ -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;
|
|
@ -1,10 +1,10 @@
|
|||||||
|
const errorHandling = require('../assets/utils/errorHandling');
|
||||||
const functionToExecute = require('../assets/functions/functionObject');
|
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);
|
const functionOutput = functionToExecute(req.params.functionName);
|
||||||
if (functionOutput !== undefined) {
|
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 });
|
||||||
}
|
}
|
Reference in New Issue
Block a user