Init Commit
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
const ErrorLocalization = require('./errorLocalization')
|
||||
const el = new ErrorLocalization()
|
||||
class AppError extends Error {
|
||||
constructor(name, statusCode, parameters) {
|
||||
super(name)
|
||||
|
||||
this.parameters = parameters
|
||||
this.name = name
|
||||
this.statusCode = statusCode
|
||||
this.status = `${statusCode}`.startsWith(4) ? 'fail' : 'error'
|
||||
this.isOperational = true
|
||||
|
||||
Error.captureStackTrace(this, this.constructor)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = AppError
|
||||
@@ -0,0 +1,5 @@
|
||||
module.exports = (fn) => {
|
||||
return (req, res, next) => {
|
||||
fn(req, res, next).catch(next)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
const errors = require('./errors.json')
|
||||
|
||||
class ErrorManager {
|
||||
constructor() {
|
||||
this.errors = errors
|
||||
}
|
||||
|
||||
getErrorText(errorName, parameters, language) {
|
||||
if (!this.errors.hasOwnProperty(errorName)) {
|
||||
return 'Error not found.'
|
||||
}
|
||||
|
||||
const error = this.errors[errorName]
|
||||
let errorMessage = ''
|
||||
|
||||
if (language === 'english') {
|
||||
errorMessage = error.english
|
||||
} else if (language === 'persian') {
|
||||
errorMessage = error.persian
|
||||
} else {
|
||||
return 'Unsupported language.'
|
||||
}
|
||||
|
||||
if (parameters) {
|
||||
for (const param in parameters) {
|
||||
if (parameters.hasOwnProperty(param)) {
|
||||
errorMessage = errorMessage.replace(
|
||||
`#${param}#`,
|
||||
parameters[param]
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errorMessage
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ErrorManager
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"NoCodeFound": {
|
||||
"code": "1",
|
||||
"english": "No code found with that ID",
|
||||
"persian": "کد مورد نظر شما پیدا نشد."
|
||||
},
|
||||
"NoUserFound": {
|
||||
"code": "2",
|
||||
"english": "No user found with that ID",
|
||||
"persian": "کاربر مورد نظر شما پیدا نشد."
|
||||
},
|
||||
"WrongPassword": {
|
||||
"code": "3",
|
||||
"english": "Entered password is incorrect",
|
||||
"persian": "رمز عبور وارد شده اشتباه است."
|
||||
},
|
||||
"CodeAlreadyUsed": {
|
||||
"code": "4",
|
||||
"english": "Entered code is already used",
|
||||
"persian": "کد وارد شده قبلا استفاده شده است."
|
||||
},
|
||||
"AccessDenied": {
|
||||
"code": "5",
|
||||
"english": "Access Denied",
|
||||
"persian": "شما دسترسی لازم برای انجام این عمل را ندارید."
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
const AppError = require('./AppError')
|
||||
const ErrorLocalization = require('./errorLocalization')
|
||||
const el = new ErrorLocalization()
|
||||
const handleCastErrorDB = (err) => {
|
||||
const message = `Invalid ${err.path} : ${err.value}`
|
||||
return new AppError(message, 400)
|
||||
}
|
||||
|
||||
const handleDuplicateFieldsDB = (err) => {
|
||||
const value = err.message.match(/(["'])(\\?.)*?\1/)[0]
|
||||
const message = `Duplicate field value: ${value}. Please use another value!`
|
||||
return new AppError(message, 400)
|
||||
}
|
||||
|
||||
const handleValidationErrorDB = (err) => {
|
||||
const errors = Object.values(err.errors).map((el) => el.message)
|
||||
const message = `Invalid input data. ${errors.join('. ')}`
|
||||
return new AppError(message, 400)
|
||||
}
|
||||
|
||||
const sendErrorDev = (err, res) => {
|
||||
res.status(err.statusCode).json({
|
||||
status: err.status,
|
||||
error: err,
|
||||
message: err.message,
|
||||
stack: err.stack,
|
||||
})
|
||||
}
|
||||
|
||||
const sendErrorProd = (err, res) => {
|
||||
// Operational, trusted error: send message to client
|
||||
if (err.isOperational) {
|
||||
res.status(err.statusCode).json({
|
||||
status: err.status,
|
||||
message: err.message,
|
||||
})
|
||||
|
||||
// Programming or other unknown error: don't leak error details
|
||||
} else {
|
||||
// 1) Log error
|
||||
console.log('ERROR', err)
|
||||
|
||||
// 2) send generic message:
|
||||
res.status(500).json({
|
||||
status: 'error',
|
||||
message: 'Something went wrong',
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (err, req, res, next) => {
|
||||
console.log(err)
|
||||
err.statusCode = err.statusCode || 500
|
||||
err.status = err.status || 'error'
|
||||
err.message = el.getErrorText(err.name, err.parameters, 'persian') || err.message
|
||||
|
||||
if (process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test') {
|
||||
sendErrorDev(err, res)
|
||||
} else if (process.env.NODE_ENV === 'production') {
|
||||
let error = { ...err }
|
||||
error.message = err.message
|
||||
|
||||
if (error.name === 'CastError') error = handleCastErrorDB(error)
|
||||
|
||||
if (error.code === 11000) error = handleDuplicateFieldsDB(error)
|
||||
|
||||
if (error.name === 'ValidationError') error = handleValidationErrorDB(error)
|
||||
|
||||
// send error to production
|
||||
sendErrorProd(error, res)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
const { AbstractClassInstantiation } = require('./internalErrors/customError')
|
||||
const { SingleTonInstanceError } = require('./internalErrors/customError')
|
||||
|
||||
module.exports = { AbstractClassInstantiation, SingleTonInstanceError }
|
||||
@@ -0,0 +1,13 @@
|
||||
class SingleTonInstanceError extends Error {
|
||||
constructor() {
|
||||
super('Instantiation of Static Functionality')
|
||||
}
|
||||
}
|
||||
|
||||
class AbstractClassInstantiation extends Error {
|
||||
constructor() {
|
||||
super('Abstract Class Cannot be Instantiated')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { SingleTonInstanceError, AbstractClassInstantiation }
|
||||
Reference in New Issue
Block a user