superagent icon indicating copy to clipboard operation
superagent copied to clipboard

Missing async trace in Node V12

Open tsingyee opened this issue 6 years ago • 1 comments

Node V12 supports full async stack trace, but superagent error stack still missing the outter async stack trace when there is error thrown from super agent, e.g a server 500 internal error. Is there anything need to be fixed from superagent side to keep the async stack trace?

at Response.toError (C:\dev\LT\wise-report-center\node_modules\superagent\lib\node\response.js:98:13)
at ResponseBase._setStatusProperties (C:\dev\LT\wise-report-center\node_modules\superagent\lib\response-base.js:119:48)
at new Response (C:\dev\LT\wise-report-center\node_modules\superagent\lib\node\response.js:44:8)
at Request._emitResponse (C:\dev\LT\wise-report-center\node_modules\superagent\lib\node\index.js:909:18)
at C:\dev\LT\wise-report-center\node_modules\superagent\lib\node\index.js:1105:42
at IncomingMessage.<anonymous> (C:\dev\LT\wise-report-center\node_modules\superagent\lib\node\parsers\json.js:22:7)
at IncomingMessage.emit (events.js:201:15)
at endReadableNT (_stream_readable.js:1130:12)
at processTicksAndRejections (internal/process/task_queues.js:84:17)

tsingyee avatar May 06 '19 10:05 tsingyee

I have a work around for now, manually keep the async stack trace before calling superagent, and add it back if necessary

let httpClient = {};
['get', 'post', 'delete', 'put'].forEach((method) => {
    let methodFn = superagent[method];
    httpClient[method] = async function (requestUrl, data, fn) {
        let responsePromise = methodFn.call(null, requestUrl, data, fn);
        let originalStack;
        try {
            throw new Error("original stack trace");
        } catch (e) {
            originalStack = e.stack;
        }
        try {
            let response = await responsePromise;
            return response.body;
        } catch (e) {
            e.fullStack = e.stack + originalStack;
            throw e;
        }
    };
});

tsingyee avatar May 06 '19 15:05 tsingyee