Skip to content

Instantly share code, notes, and snippets.

@lll000111
Last active July 5, 2019 11:12
Show Gist options
  • Save lll000111/7c4fdfc35e9f2c354554d17b71dc8ae2 to your computer and use it in GitHub Desktop.
Save lll000111/7c4fdfc35e9f2c354554d17b71dc8ae2 to your computer and use it in GitHub Desktop.
V8 Zero-cost stack trace test/issue/question demo code
/* node.js v12.6.0, TypeScript - i.e. ES2018 + type annotations */
/*
===============================================
OLD VERSION (using fs.readFile)
===============================================
With this function I did NOT get a full async stacktrace. Example:
FileNotFoundError: File not found: c3ba0f23b58a5c645b7f3e803b1ba5af42d8d2f9262b029d2718a89709ba0b03 [objects]
at /home/mha/Projects/replicant/node_modules/one.core/lib/system/storage-base.js:155:65
at async readUTF8TextFile (node_modules/one.core/lib/system/storage-base.js:153:10)
*/
export function readUTF8TextFile(filename: string): Promise<string> {
return new Promise((resolve, reject): void => {
readFile(join(STORAGE_DIR[type], filename), 'utf8', (err, contents) => {
if (err) {
reject(err);
} else {
resolve(contents);
}
});
});
}
/*
===============================================
NEW VERSION (using fs.promise.readFile)
===============================================
NOTE AND THAT IS THE MAIN QUESTION: I *must* use promise.cath(err => ...).
If I use try/catch it does not work.
With this function I get a full async stacktrace. Example:
FileNotFoundError: File not found: c3ba0f23b58a5c645b7f3e803b1ba5af42d8d2f9262b029d2718a89709ba0b03 [objects]
at /home/mha/Projects/replicant/node_modules/one.core/lib/system/storage-base.js:155:65
at async readUTF8TextFile (node_modules/one.core/lib/system/storage-base.js:153:10)
at async calculateIdHash (node_modules/one.core/lib/microdata-to-id-hash.js:104:21)
at async getIdHash (node_modules/one.core/lib/storage-id-hash-cache.js:33:18)
at async /home/mha/Projects/replicant/node_modules/one.core/lib/storage-references-handler.js:31:20
at async Promise.all (index 1)
at async referenceObjectsHandler (node_modules/one.core/lib/storage-references-handler.js:40:3)
at async Promise.all (index 0)
at async storeVersionedObject (node_modules/one.core/lib/storage-versioned-objects.js:67:25)
at async storeVersionedObject (node_modules/one.core/lib/plan-storage-api.js:52:12)
at async Promise.all (index 0)
at async Object.createObjects (node_modules/one.core/lib/identity.js:19:10)
at async createNewPlanResult (node_modules/one.core/lib/plan.js:144:23)
at async createObjectsWithPurePlan (node_modules/one.core/lib/plan.js:223:10)
at async createSingleObjectThroughPurePlan (node_modules/one.core/lib/plan.js:250:22)
at async Context.<anonymous> (test/jsapi-test.js:233:27)
*/
export async function readUTF8TextFile(filename: string): Promise<string> {
return await readFile(join(STORAGE_DIR[type], filename), 'utf8').catch(err => {
throw new Error(err);
});
}
/*
A VARIANT OF THE NEW VERSION USING try/catch
which does not produce a stack trace
*/
export async function readUTF8TextFile(filename: string): Promise<string> {
try {
return await readFile(join(STORAGE_DIR[type], filename), 'utf8');
} catch (err) {
throw new Error(err);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment