在我即将发生的错误中,我只是在nodejs中尝试使用Apache Thrift,然后才遇到该错误。
这是我的demo.thrift
文件
namespace js demo
typedef i32 int
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
struct Work {
1: int num1 = 0,
2: int num2,
3: Operation op,
4: optional string comment
}
exception InvalidOperation {
1: int message,
2: string trace
}
service Calculator {
void ping()
double calculate(1: int logid, 2: Work w) throws (1: InvalidOperation oops),
oneway void zip()
}
这里是server.js
我使用开关大小写来确定server.js
中的操作
// inside thrift.createServer
calculate: (logid, work, result) => {
let answer = null, oops = null;
switch(work.op) {
// Code related to Operation.ADD, Operation.SUBTRACT ...
default: {
console.log("ERROR!");
oops = InvalidOperation();
oops.message = work.op;
oops.trace = "Unknown Operation";
}
}
result(oops, answer);
}
client.js
用calculate(12345, { num1:1, num2:2, op: 10 })
呼叫服务器时
抛出错误而不是返回错误
TypeError: Cannot set property 'name' of undefined in demo_types.js:122
demo_types.js
中与InvalidOperation相关的部分是
// Work related code
var InvalidOperation = module.exports.InvalidOperation = function(args) {
Thrift.TException.call(this, "InvalidOperation");
this.name = "InvalidOperation"; // points to here
this.message = null;
this.trace = null;
if (args) {
if (args.message !== undefined && args.message !== null) {
this.message = args.message;
}
if (args.trace !== undefined && args.trace !== null) {
this.trace = args.trace;
}
}
};
Thrift.inherits(InvalidOperation, Thrift.TException);
InvalidOperation.prototype.name = 'InvalidOperation';
// InvalidOperation.read & .write
知道为什么会引发错误吗?
答案 0 :(得分:2)
实际上,我意识到了为什么会引发此错误。这是一个古老的Javascript错误。
oops = new InvalidOperation();
就是这样。