1

I'm trying to serialize an exception in an AWS lambda function, but when I do it, the object has no properties. Here is a simplified version of this:

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);
  }
};

Here's the log output. Note that when I JSON.stringify the exception object, there's nothing there, but in the context.fail line, the exception data is there.

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   

Anyone know what might be happening?

1
  • Create custom output handler and that will solve your problem. Commented Sep 9, 2015 at 23:20

2 Answers 2

1

The properties of the Exception might have been defined with Object.defineProperty. if you run the following example, you'll see that they don't get serialized

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)); // {}
Sign up to request clarification or add additional context in comments.

3 Comments

Good thought, but console.log(err.errorMessage) returns undefined. :(
Interesting, what do the following things output with console.log: err.constructor, Object.getOwnPropertyNames(err), Object.keys(err) ?
err.constructor=function ReferenceError() { [native code] }, getOwnPropertyNames=stack,arguments,type,message. So then I tried each of those 4 properties and they have their own values. So it looks like context.fail must take the native properties and then construct its own JSON for output. I don't know why I can't see those when I stringify the object, but at least I can figure out to get the data I need. Thanks!
0

Just found a very simple stupid option(s).

1 Prepend output with a string

console.error("message": "_" + error);

2 Use toString

console.error("message": error.toString());

3 Use message property

console.error("message": error.message);

All three work for me but might not work for you depending on your use case.

Hope that helps

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.