Last active
July 6, 2019 20:55
-
-
Save lll000111/534f7a4a3b0c7ac6ff672b500fe9f3e9 to your computer and use it in GitHub Desktop.
function version 1 creates a full async stack trace, version 2 does not
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==================================================================== | |
// CODE FOR issue https://bugs.chromium.org/p/v8/issues/detail?id=9443 | |
// ==================================================================== | |
const fs = require('fs'); | |
const readFile = fs.promises.readFile; | |
/* | |
#### RESULTS #### | |
## With readVersion1 | |
$ node test.js | |
Error: Error: ENOENT: no such file or directory, open 'NonExistingFile' | |
at /home/mha/Projects/core/test.js:7:15 | |
at async readUTF8TextFileV1 (/home/mha/Projects/core/test.js:5:12) | |
at async doSomething (/home/mha/Projects/core/test.js:20:12) | |
at async doSomethingEvenHigherLevel (/home/mha/Projects/core/test.js:25:12) | |
## With readVersion2 | |
$ node test.js | |
Error: Error: ENOENT: no such file or directory, open 'NonExistingFile' | |
at readUTF8TextFileV2 (/home/mha/Projects/core/test.js:15:15) | |
*/ | |
async function readVersion1(filename) { | |
return await readFile(filename, 'utf8') | |
.catch(err => { | |
throw new Error(err); | |
}); | |
} | |
async function readVersion2(filename) { | |
try { | |
return await readFile(filename, 'utf8'); | |
} catch (err) { | |
throw new Error(err); | |
} | |
} | |
async function doSomething(filename) { | |
return await readVersion1(filename); | |
// return await readUTF8TextFileV2(filename); | |
} | |
async function doSomethingEvenHigherLevel (filename) { | |
return await doSomething(filename); | |
} | |
doSomethingEvenHigherLevel('NonExistingFile') | |
.catch(err => console.error(err)); | |
// ==================================================================== | |
// To provide sample code for my followup comments: | |
// The following function produced a full stack trace without having | |
// to wrap and rethrow the node.js error in "throw new Error(err)" | |
// HOWEVER, manually promisifying by wrapping the entire body in | |
// return new Promise((resolve,reject) => {....}); | |
// results in NO stack trace. | |
// | |
// Wrapping randomBytesPromisified() in try/catch and rethrowing the | |
// error from the catch also works here. | |
// ==================================================================== | |
const {randomBytes} = require('crypto'); | |
const {promisify} = require('util'); | |
const randomBytesPromisified = promisify(randomBytes); | |
async function createRandomString(length = 64) { | |
return await randomBytesPromisified(length).then(buf => { | |
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-'; | |
for (const [i, v] of buf.entries()) { | |
buf[i] = CHARS.charCodeAt(v % 64); | |
} | |
throw new Error('FORCE FAILURE'); | |
//return buf.toString('utf8'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment