I want to copy Javascript Error
without reference.
Example:
const error1 = new Error('error1');
const error2 = error1;
error2.message = 'error2';
console.log(error1);
console.log(error2);
Output:
Error: error2
at evalmachine.<anonymous>:1:16
at Script.runInContext (vm.js:133:20)
at Object.runInContext (vm.js:311:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at ReadStream.Readable.push (_stream_readable.js:224:10)
at lazyFs.read (internal/fs/streams.js:181:12)
Error: error2
at evalmachine.<anonymous>:1:16
at Script.runInContext (vm.js:133:20)
at Object.runInContext (vm.js:311:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at ReadStream.Readable.push (_stream_readable.js:224:10)
at lazyFs.read (internal/fs/streams.js:181:12)
When i change error2
then error1
will also change. then I tried lodash clone
feature. But unfortunately it instead returns an object, not an instance of Error. is there a way so that I can copy an Error
without reference and return an instanceof Error
without changing stack
or other default properties?