endojs / endo

Endo is a distributed secure JavaScript sandbox, based on SES
Apache License 2.0
829 stars 72 forks source link

Error stack string are not consistent #1810

Open mhofman opened 1 year ago

mhofman commented 1 year ago

Describe the bug

Depending on errorTaming and the JS platform, the stack string values are not quite consistent.

Expected behavior

Error.getStackString() should return a string matching the proposal. In pseudo code

const errorString = call(getIntrinsic('Error.prototype.toString'), error);
const errorFramesString = frames.map(frame => `\n frame`).join('');
return `${errorString}${errorFramesString}`;

In particular: the error details, then on a new line each, stack frame strings prefixed by a space, with no trailing new line (or space) at the end of the overall string.

Steps to reproduce

Run the following with eshost:

// First line
const makeError = msg => Error(msg);
const err = makeError('my message');
err.name = 'CustomError';
err.toString = () => 'overriden';

print(`%%%${err.stack}%%%`);
#### JavaScriptCore
%%%makeError@/tmp/iG9Fhho45sqTzR2t5UO5/f-1696624184583-9045-1v3jfly.08s3.js:2:2222
global code@/tmp/iG9Fhho45sqTzR2t5UO5/f-1696624184583-9045-1v3jfly.08s3.js:3:22%%%

#### Moddable XS
%%%CustomError: my message
 at makeError (/tmp/3h6En0rXJ9qeV80kfdUL/f-1696624184529-9045-gb7iqc.x9dy9.js:16)
 at (/tmp/3h6En0rXJ9qeV80kfdUL/f-1696624184529-9045-gb7iqc.x9dy9.js:17)%%%

#### SpiderMonkey
%%%makeError@/tmp/9Zl3zp4fFEW8J8HMSw4l/f-1696624184592-9045-183o0we.gm59j.js:2:8095
@/tmp/9Zl3zp4fFEW8J8HMSw4l/f-1696624184592-9045-183o0we.gm59j.js:3:22
%%%

#### V8
%%%CustomError: my message
    at makeError (/tmp/FiORUnWr2NZJAm7DpKRK/f-1696624184560-9045-1b24p8y.tdtt.js:2:5916)
    at /tmp/FiORUnWr2NZJAm7DpKRK/f-1696624184560-9045-1b24p8y.tdtt.js:3:13%%%

Observed behavior

JavaScriptCore and SpiderMonkey seem to omit the error details, and stack frames are not prefixed by spaces. SpiderMonkey always seem to have a trailing new line.

XS and v8 stack traces have similar formats. They both include the error details, but have varied amount of head white space for the stack frame lines, followed by the string at before the stack frame string.

Open question

Should we model the frame string against Moddable / XS, and include the string at to avoid rewriting stacks on our main platforms.