在我们的 NodeJS 应用程序中,我们通过扩展默认的错误对象来定义自定义错误类:
"use strict";
const util = require("util");
function CustomError(message) {
Error.call(this);
Error.captureStackTrace(this, CustomError);
this.message = message;
}
util.inherits(CustomError, Error);
这允许我们抛出 CustomError("Something");
并正确显示堆栈跟踪,instanceof Error
和 instanceof CustomError
正常工作。
但是,为了在我们的 API 中返回错误(通过 HTTP),我们希望将错误转换为 JSON。在出现错误时调用 JSON.stringify()
会导致 “{}”
,这对于消费者来说显然不是真正的描述性内容。
为了解决这个问题,我考虑重写 CustomError.prototype.toJSON()
,以返回一个包含错误名称和消息的对象字面量。 JSON.stringify()
将把这个对象字符串化,一切都会很好:
// after util.inherits call
CustomError.prototype.toJSON = () => ({
name : "CustomError",
message : this.message
});
但是,我很快发现这会引发 TypeError: Cannot assign to read only property 'toJSON' of Error
。这可能是有道理的,因为我正在尝试写入原型(prototype)。所以我改为更改构造函数:
function CustomError(message) {
Error.call(this);
Error.captureStackTrace(this, CustomError);
this.message = message;
this.toJSON = () => ({
name : "CustomError",
message : this.message
});
}
这样(我预计),将使用 CustomError.toJSON 函数并忽略 CustomError.prototype.toJSON(来自 Error)。
不幸的是,这只会在对象构造时抛出错误:无法分配给 CustomError 的只读属性“toJSON”
。
接下来,我尝试从文件中删除 "use strict";
,这某种程度上解决了问题,因为不再抛出任何错误,尽管
函数。JSON.stringify()
根本没有使用 toJSON()
在这一点上,我只是绝望,只是尝试随机的事情。最终我最终使用 Object.defineProperty()
而不是直接分配给 this.toJSON
:
function CustomError(message) {
Error.call(this);
Error.captureStackTrace(this, CustomError);
this.message = message;
Object.defineProperty(this, "toJSON", {
value: () => ({
name : "CustomError",
message : this.message
})
});
这非常有效。在严格模式下,没有错误被调用,并且 JSON.stringify()
返回 {"name:"CustomError", "message": "Something"}
就像我想要的
所以尽管它现在可以正常工作,但我仍然想知道:
- 为什么这行得通?我希望它等同于
this.toJSON = ...
但显然不是。 - 它应该像这样工作吗? IE。依赖这种行为是否安全?
- 如果不是,我该如何正确重写 toJSON 方法? (如果可能的话)
最佳答案
Why does this work exactly?
Object.defineProperty
只是定义一个属性(或者改变它的属性,如果它已经存在并且是可配置的)。与赋值 this.toJSON = …
不同,它不关心任何继承,也不检查是否存在可能是 setter 或不可写的继承属性。
Should it work like this? I.e. is it safe to depend on this behaviour?
是的,是的。也许您甚至可以在原型(prototype)上使用它。
对于您的实际用例,给定最新的 node.js 版本,使用 extends Error
的 ES6 类以获得最佳结果。
关于javascript - Object.defineProperty 覆盖只读属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32821837/