Got: Async stack traces for callback errors

Created on 19 Feb 2020  ·  13Comments  ·  Source: sindresorhus/got

What problem are you trying to solve?

I need to know the caller that generated a given http error.

Describe the feature

I use node 13 which has async stack traces but I don't have the caller stack trace when using got. Instead I get this stack:

    err HTTPError: Response code 404 (Not Found)
        at EventEmitter.<anonymous> (.../node_modules/got/dist/
source/as-promise.js:118:31)
        at processTicksAndRejections (internal/process/task_queues.js:97:5) {
      name: 'HTTPError'
    }

I guess this happens because the connection can be used by multiple callers but there should be a way to have the whole stack.

documentation ✭ help wanted ✭

Most helpful comment

All 13 comments

This works as expected: EventEmitter.<anonymous> is the handler for the response event received from the native Node.js call.

Related: #795

I don't think we can do much about this. This is just how Node.js works. One possible solution would be to create a stacktrace before making the request and create another one when erroring and then merge these two.

@sindresorhus What do you think?

We could, but:

  1. the API is marked experimental so it will give a warning (it's live since Node.js 8.1) - but I think it's stable for this use case as I don't see any issues that would break this
  2. the change would be visible globally.

I think we should create another .md file and show how to achieve the desired result step-by-step.

One possible solution would be to create a stacktrace before making the request and create another one when erroring and then merge these two.

@szmarczak This approach works. Our team used in many projects that involved multiplexing a connection or a event emitter. The only think I would worry about is the CPU load associated with generating the stack.

Maybe stack trace generation performance has improved in the last years. I have seen some articles talking about zero cost async stack traces but I'm not sure if it applies:

@aalexgabi The zero-cost stacktraces you mentioned are related to async/await (and are already on by default), not I/O operations.

the change would be visible globally.

Yeah, we cannot modify anything globally.

But would it have to be global? I was thinking of storing the stack in the async context and then merge it on error.

I think we should create another .md file and show how to achieve the desired result step-by-step.

👍

But would it have to be global? I was thinking of storing the stack in the async context and then merge it on error.

That would work too, but the question is how much performance you would have to give up, since on every async task there would be called Error.captureStackTrace...

Maybe we should just make an independent module that creates long stack traces using async hooks contexts and then recommend that, but warn that it might have a performance impact. Then the user can decide whether it's worth it or not.

Would the --async-stack-traces option in nodejs12 solve this issue?
http://thecodebarbarian.com/async-stack-traces-in-node-js-12.html

@PSeitz It's already on by default IIRC.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dAnjou picture dAnjou  ·  3Comments

pvdlg picture pvdlg  ·  3Comments

darksabrefr picture darksabrefr  ·  3Comments

astoilkov picture astoilkov  ·  3Comments

quocnguyen picture quocnguyen  ·  4Comments