1'use strict'; 2 3function assign(obj, props) { 4 for (const key in props) { 5 Object.defineProperty(obj, key, { 6 value: props[key], 7 enumerable: true, 8 configurable: true, 9 }); 10 } 11 12 return obj; 13} 14 15function createError(err, code, props) { 16 if (!err || typeof err === 'string') { 17 throw new TypeError('Please pass an Error to err-code'); 18 } 19 20 if (!props) { 21 props = {}; 22 } 23 24 if (typeof code === 'object') { 25 props = code; 26 code = undefined; 27 } 28 29 if (code != null) { 30 props.code = code; 31 } 32 33 try { 34 return assign(err, props); 35 } catch (_) { 36 props.message = err.message; 37 props.stack = err.stack; 38 39 const ErrClass = function () {}; 40 41 ErrClass.prototype = Object.create(Object.getPrototypeOf(err)); 42 43 return assign(new ErrClass(), props); 44 } 45} 46 47module.exports = createError; 48