自定义错误格式
Custom error formatting
>我已经实现了自己的自定义错误:
function MyError() {
var temp = Error.apply(this, arguments);
temp.name = this.name = 'MyError';
this.stack = temp.stack;
this.message = temp.message;
}
MyError.prototype = Object.create(Error.prototype, {
constructor: {
value: MyError,
writable: true,
configurable: true
}
});
我缺少的是让它在屏幕上显示自己,就像发生常规的未手动错误时一样,即如果我们执行throw new Error('Hello!')
,我们会得到输出:
throw new Error('Hello!');
^
Error: Hello!
at Object.<anonymous> (D:'NodeJS'tests'test1.js:28:7)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
at node.js:974:3
现在,当我这样做时,我想要同样格式良好的输出:
try {
throw new MyError("Ops!");
} catch (e) {
console.log(e);
}
但相反,我得到的是:
{ [MyError: Ops!]
name: 'MyError',
stack: 'MyError: Ops!'n at MyError.Error (native)'n at new MyError (D:''NodeJS''tests''test1.js:2:22)'n at Object.<anonymous> (D:''NodeJS''tests''test1.js:22:11)'n at Module._compile (module.js:425:26)'n at O
bject.Module._extensions..js (module.js:432:10)'n at Module.load (module.js:356:32)'n at Function.Module._load (module.js:313:12)'n at Function.Module.runMain (module.js:457:10)'n at startup (node.js:138:18)'n
at node.js:974:3',
message: 'Ops!' }
还需要执行哪些操作才能使console.log(e)
输出相同格式良好的演示文稿,以便自动MyError
,而不必使用显式e.stack
引用?
更新:起初我看到了一些关于要实现的方法toJSON
的建议,我做到了,但它并没有完全奏效。我假设必须有一个可覆盖的方法,console.log
使用这种方法来格式化错误对象,但它是什么呢?
而不是覆盖 toJSON
方法(或者,toString
通常假设的那样(,您应该覆盖 inspect
,这是在尝试在 V8 中console.log
对象时使用的。
例如:
MyError.prototype.inspect = function () {
return this.stack;
};
应该可能可以解决问题。
试试这个
try {
throw new MyError("Ops!");
} catch (e) {
console.log(e.stack);
}
如果您在浏览器的控制台中执行此代码,它将如下所示:
MyError: Ops!
at MyError.Error (native)
at new MyError (<anonymous>:3:22)
at <anonymous>:18:11
at Object.InjectedScript._evaluateOn (<anonymous>:875:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:808:34)
at Object.InjectedScript.evaluate (<anonymous>:664:21)
如果我理解正确,您想要的是打印错误的堆栈?
我编写了一个函数来处理未捕获的异常错误,以便服务器不会在未处理的错误时停止。
功能
function errorHandler (){
process.on('uncaughtException', function (err) {
console.log(err); // Displays the Error;
console.error((new Date).toUTCString() + ' uncaughtException:', err.message); // Displays the Date and Error Message.
console.error(err.stack); // Stack in Which the error occurred.
});
}
输出
ISODate("2015-07-28T04:56:20.000Z") uncaughtException: Cannot read property 'asset' of undefined
TypeError: Cannot read property 'asset' of undefined'n
at /Path/file.js:31:31'n
at /path/node_modules/mongodb/lib/mongo_client.js:436:11'n
at process._tickDomainCallback (node.js:463:13)
希望这有帮助。
这不是
很推荐,但您可以随时挂钩console.log
处理程序:
var oldhandler = console.log;
console.log = function () {
if (typeof(arguments[0].stack) != "undefined" && arguments[0] instanceof(Error) && typeof(arguments[0].stack) != "undefined") {
oldhandler.apply(console,[arguments[0].stack]);
} else {
oldhandler.apply(console,arguments);
}
}
try {
throw new Error("qweqew");
} catch (ex) {
console.log(ex);
}
小提琴:http://jsfiddle.net/xagpf5ff/
相关文章:
- 如何在不创建格式错误的HTML标记的情况下分解字符串
- JSON字符串格式错误
- Ckeditor出现格式错误的html问题
- 如何用x射线刮除格式错误的HTML代码
- 阻止Google Chrome更正格式错误的HTML脚本
- 使用RegEx在javascript中重写格式错误的日期yyyy-MM-d到yyyy-MM-dd
- PDFKit:PNG的未知图像格式错误
- 解析格式错误的 json 字符串,该字符串周围没有双引号(Java 脚本)
- 加载 pkcs8 会在 jsrsasign 中抛出格式错误的格式
- 格式错误,遇到符号
- 剑道格式错误
- 使用 jquery ajax 请求和日期格式错误解析两个参数
- JWT 未解码“JWT 格式错误” - 节点角度
- Web 浏览器对看似格式错误的文件的容忍度
- Python Pandas read_excel 和 to_json 日期格式错误
- ActionController::未知格式错误
- firefox格式错误
- 主干提取url数据格式错误
- 正在分析JavaScript中格式错误的JSON
- Javascript格式错误