This is an automated email from the ASF dual-hosted git repository. jamesthomas pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-runtime-nodejs.git
The following commit(s) were added to refs/heads/master by this push: new 3ede534 Fixes #63 - Support errors thrown from async functions (#95) 3ede534 is described below commit 3ede5341afed18e617140de243a0c8050293f74a Author: James Thomas <jthomas...@gmail.com> AuthorDate: Mon Oct 29 14:19:07 2018 +0000 Fixes #63 - Support errors thrown from async functions (#95) Return error string from errors and log stack trace for rejected promises. --- core/nodejsActionBase/runner.js | 6 +++- .../NodeJs8ActionContainerTests.scala | 40 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/core/nodejsActionBase/runner.js b/core/nodejsActionBase/runner.js index ea869be..1ad7172 100644 --- a/core/nodejsActionBase/runner.js +++ b/core/nodejsActionBase/runner.js @@ -114,7 +114,11 @@ function NodeActionRunner() { if (!error) { resolve({ error: {}}); } else { - resolve({ error: error }); + // Log stack trace for rejected promises. + if (error.message) { + console.error(error) + } + resolve({ error: error.toString() }); } }); } diff --git a/tests/src/test/scala/runtime/actionContainers/NodeJs8ActionContainerTests.scala b/tests/src/test/scala/runtime/actionContainers/NodeJs8ActionContainerTests.scala index 61fa8df..c487b89 100644 --- a/tests/src/test/scala/runtime/actionContainers/NodeJs8ActionContainerTests.scala +++ b/tests/src/test/scala/runtime/actionContainers/NodeJs8ActionContainerTests.scala @@ -52,4 +52,44 @@ class NodeJs8ActionContainerTests extends NodeJsNonConcurrentTests { } } + it should "support errors thrown from async functions" in { + withNodeJsContainer { c => + val code = """ + | async function main() { + | return a.b.c + | } + """.stripMargin; + + val (initCode, _) = c.init(initPayload(code)) + initCode should be(200) + + val (runCode, runRes) = c.run(runPayload(JsObject())) + runCode should be(200) // action writer returning an error is OK + + runRes shouldBe defined + runRes.get.fields.get("error") shouldBe defined + runRes.get.fields("error").toString.toLowerCase should include("referenceerror: a is not defined") + } + } + + it should "support user thrown errors from async functions" in { + withNodeJsContainer { c => + val code = """ + | async function main() { + | throw new Error('app error') + | } + """.stripMargin; + + val (initCode, _) = c.init(initPayload(code)) + initCode should be(200) + + val (runCode, runRes) = c.run(runPayload(JsObject())) + runCode should be(200) // action writer returning an error is OK + + runRes shouldBe defined + runRes.get.fields.get("error") shouldBe defined + runRes.get.fields("error").toString.toLowerCase should include("error: app error") + } + } + }