I am trying to repair my relationship with JavaScript after a long time of conflict :D
In the following code :
class e extends Error{
constructor(message,id){
super(message)
this.id = id
}
}
const e1 = new e('message',12);
const e2 = {...e1}
console.log(e2);
}
the result is {id:12}
As you can see the message property is not there ,I understand that this happening because the message property is not owned by e2.
My question is what is the best way to make it so such that the result is {id:12,message:'message'}
I understand that it may be very novice question, but I appreciate your elaboration on it
use case
I have a global error handler below I need to copy err object into error object if in production mode, but I found that I need to manually add this line error.message = err.message;
I was wondering if there is away to clone err object without manually add this line i.e. what is the best way of cloning an object without knowing its super class properties?
module.exports = (err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.status = err.status || 'error';
console.log(process.env.NODE_ENV)
if (process.env.NODE_ENV === 'development') {
sendErrorDev(err, res);
} else if (process.env.NODE_ENV === 'production') {
let error = { ...err };
error.message = err.message;
// console.log(err)
// if (error.name === 'CastError') error = handleCastErrorDB(error);
if (error.code === 11000) error = handleDuplicateFieldsDB(error);
// if (error.name === 'ValidationError')
// error = handleValidationErrorDB(error);
sendErrorProd(error, res);
}
};