16

Let's say I have 12 async/await functions, and on the 12th one deep, an error happens. Right now, I have this code to catch all errors:

process.on('unhandledRejection', function {
    console.error(err);
    process.exit(1);
});

The problem is, the stacktrace is not returned:

ReferenceError: sdfg is not defined
- get.js:29 Fruit.module.exports [as get]
  /project/models/fruit/get.js:29:2
- next_tick.js:129 process._tickDomainCallback
  internal/process/next_tick.js:129:7

On other projects, when I used callbacks with the structure of:

function doSomething(err, done) {
  if (err) { return done(err); }
  /* do something */
  return done(null, true);
}

Then I had a nice stack trace of where the error has occured and the steps that led there. Now with async/await I've tried catching errors at all kinds of levels with no result. I've also tried longjohn and stackup -- and I still get only the last function that threw the error.

Help -- how do I see the complete stack?! And what's the proper way to catch nested async/await errors?

EDIT: (a complete example)

const getA = async () => {
    await getB();
}

const getB = async () => {
    await getC();
    sdgf();
}

const getC = async () => {}

const start = async () => {
    await getA();
}

start().then().catch(e => console.error(e));

3 Answers 3

4

This issue appears to be resolved in Node 12. With v12.6.0, the snippet in the original post yields the following stack trace:

ReferenceError: sdgf is not defined
    at getB (/path/to/test.js:7:3)
    at async getA (/path/to/test.js:2:3)
    at async start (/path/to/test.js:15:3)
Sign up to request clarification or add additional context in comments.

Comments

2

It is not possible to get the stack trace after the first await statement, in an async function, as of today.

https://github.com/nodejs/node/issues/11865

Comments

1

unhandledRejection error handler might not be the right way to try to catch all these. I'd suggest wrapping your async/awaits in try/catch blocks:

async function doSomething() {
  try {
    await doSomethingElse()
  } catch(err) {
    console.log(err)
  }
}

This should give you a better stack trace.

8 Comments

This is what that gives: ReferenceError: sdfg is not defined at Fruit.module.exports [as get] (/models/fruit/get.js:29:2) at process._tickDomainCallback (internal/process/next_tick.js:129:7)
@JohnDerring that looks like a stack trace, saying that sdfg is not defined at line 29 of the fruit model. What am I missing?
The 11 other functions that came before getting to this function? In other words the complete stack trace...?
@JohnDerring is the sdfg error happening on fruit/get.js line 29, or is it happening somewhere else?
Yes, exactly. On fruit/get.js:29 I put a sdfg() just to throw an error, so that's the correct place. The problem is it takes 11 other async/await functions to get to this place, and the error stack is just showing 2 places: the fruit/get.js and the next_tick). I'm confused about how to show the complete error stack...
|

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.