AWS lambda函数中的异常为空

时间:2015-09-08 22:49:31

标签: node.js aws-lambda

我正在尝试在AWS lambda函数中序列化异常,但是当我这样做时,该对象没有属性。以下是此简化版本:

exports.handler = function(event, context) {
  try {
    var x = some.property;
    context.succeed(null);
  } catch (err) {
    console.log("error=" + JSON.stringify(err, null, 2));
    context.fail(err);
  }
};

这是日志输出。请注意,当我JSON.stringify异常对象时,没有任何内容,但在context.fail行中,异常数据就在那里。

START RequestId: 810913d1-567b-11e5-a0d1-95dad6b9184b
2015-09-08T22:46:55.308Z    810913d1-567b-11e5-a0d1-95dad6b9184b    error={}
2015-09-08T22:46:55.368Z    810913d1-567b-11e5-a0d1-95dad6b9184b    {"errorMessage":"some is not defined","errorType":"ReferenceError","stackTrace":["exports.handler (/var/task/index.js:5:17)"]}
END RequestId: 810913d1-567b-11e5-a0d1-95dad6b9184b
REPORT RequestId: 810913d1-567b-11e5-a0d1-95dad6b9184b  Duration: 174.72 ms Billed Duration: 200 ms     Memory Size: 128 MB Max Memory Used: 9 MB   

任何人都知道可能会发生什么?

1 个答案:

答案 0 :(得分:0)

可能已使用Object.defineProperty定义了Exception的属性。如果您运行以下示例,您将看到他们没有被序列化

function Exception () {
    this.message = function () {
        return 'Oh NOES';
    };
}

Exception.prototype.constructor = Exception;

Object.defineProperty(Exception.prototype, 'foo', {
    get: function () {
        return this.message();
    }
});

var e = new Exception();

console.log(e.foo) // 'Oh NOES'
console.log(JSON.stringify(e, null, 2)); // {}
相关问题