[英]How to clone a javascript Error into a standard object
Specifically, I'd like to convert a Javascript error to an object whose properties can be listed, and hence console.logged. 具体来说,我想将Javascript错误转换为可以列出其属性的对象,并因此将console.logged。
try {
throw new Error('418 : Blue Teapot of Death');
} catch (error) {
for (var k in arg) { console.log('Key found: ' + k); } // Outputs nothing
}
EDIT : 编辑 :
Using @YeahBoy solution, there is the final solution I adopted, using lodash pick function : 使用@YeahBoy解决方案,是我采用的最终解决方案,使用lodash pick函数:
var copy_with_enumerable_properties = function (obj) {
var props = Object.getOwnPropertyNames(obj); // Include non-enumerable properties
return _.pick(obj, props);
};
try {
throw new Error('418 : Blue Teapot of Death');
} catch (error) {
var error_obj = copy_with_enumerable_properties(error);
error_obj.stack = error_obj.stack.split('\n');
console.log(JSON.stringify(error_obj, null, 2));
}
Try this. 尝试这个。
try {
throw new Error('418 : Blue Teapot of Death');
} catch (error) {
var prop = Object.getOwnPropertyNames(error);
for (var k in prop) { console.log('Key found: ' + prop[k]); }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.