Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Stacktrace lost on HTTPError #447

Closed
sparebytes opened this issue Jan 15, 2018 · 3 comments
Closed

Async Stacktrace lost on HTTPError #447

sparebytes opened this issue Jan 15, 2018 · 3 comments

Comments

@sparebytes
Copy link

The async stacktrace isn't very helpful when an HTTPError is thrown. On the other hand a timeout which throws a RequestError keeps the relevant stack info.

Here is example code that runs on Node 8:

const got = require('got');

async function gotStackTraceLost() {
    try {
        const z = await got.post('https://google.com/bad-endpoint');
    }
    catch (e) {
        console.error("\n\nGOT Error and lost stacktrace!", e);
    }
}

async function gotStackTraceKept() {
    try {
        const z = await got.post('https://127.0.0.1:9999/bad-endpoint', { timeout: 1 });
    }
    catch (e) {
        console.error("\n\nGOT Error and kept stacktrace!", e);
    }
}

async function controlTest() {
    try {
        throw new Error("This is a test");
    }
    catch (e) {
        console.error("\n\nControl Error!", e);
    }
}

gotStackTraceLost();
gotStackTraceKept();
controlTest();
@brandon93s
Copy link
Contributor

Got now maintains the stacktrace as expected for the HTTPError:

image

Note that you can also set throwHttpErrors to false if you'd prefer to handle these yourself as a response.

Tested against master.

@szmarczak
Copy link
Collaborator

szmarczak commented Jul 7, 2018

@brandon93s No and yes. No, because the stacktrace should show more details. It's because setImmediate is used. But that's how Node.JS deals with that, so I don't think we can help with that.

    const EventEmitter = require('events');

    const givesPromise = () => new Promise((resolve, reject) => {
        const ee = new EventEmitter();

        ee.once('error', e => {
            reject(e);
        });
        // ee.emit('error', new Error('stacktrace not lost')); return; // uncomment this line and you'll see it gives more info
        setImmediate(async _ => {
            ee.emit('error', new Error('stacktrace lost'));
        })
    });

    (async function() {
        try {
            await givesPromise();
        } catch (e) {
            console.log(e);
        }
    })();

@sindresorhus
Copy link
Owner

Relevant Node.js issue: nodejs/node#11865

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants