| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 07f1efc commit c5f54b1
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -285,14 +285,16 @@ function REPLServer(prompt, | |||
| 285 | 285 | self._domain.on('error', function debugDomainError(e) { | |
| 286 | 286 | debug('domain error'); | |
| 287 | 287 | const top = replMap.get(self); | |
| 288 | - | ||
| 288 | + const pstrace = Error.prepareStackTrace; | ||
| 289 | + Error.prepareStackTrace = prepareStackTrace(pstrace); | ||
| 289 | 290 | internalUtil.decorateErrorStack(e); | |
| 291 | + Error.prepareStackTrace = pstrace; | ||
| 290 | 292 | const isError = internalUtil.isError(e); | |
| 291 | 293 | if (e instanceof SyntaxError && e.stack) { | |
| 292 | 294 | // remove repl:line-number and stack trace | |
| 293 | 295 | e.stack = e.stack | |
| 294 | - .replace(/^repl:\d+\r?\n/, '') | ||
| 295 | - .replace(/^\s+at\s.*\n?/gm, ''); | ||
| 296 | + .replace(/^repl:\d+\r?\n/, '') | ||
| 297 | + .replace(/^\s+at\s.*\n?/gm, ''); | ||
| 296 | 298 | } else if (isError && self.replMode === exports.REPL_MODE_STRICT) { | |
| 297 | 299 | e.stack = e.stack.replace(/(\s+at\s+repl:)(\d+)/, | |
| 298 | 300 | (_, pre, line) => pre + (line - 1)); | |
@@ -375,6 +377,30 @@ function REPLServer(prompt, | |||
| 375 | 377 | }; | |
| 376 | 378 | } | |
| 377 | 379 | ||
| 380 | + function filterInternalStackFrames(error, structuredStack) { | ||
| 381 | + // search from the bottom of the call stack to | ||
| 382 | + // find the first frame with a null function name | ||
| 383 | + if (typeof structuredStack !== 'object') | ||
| 384 | + return structuredStack; | ||
| 385 | + const idx = structuredStack.reverse().findIndex( | ||
| 386 | + (frame) => frame.getFunctionName() === null); | ||
| 387 | + | ||
| 388 | + // if found, get rid of it and everything below it | ||
| 389 | + structuredStack = structuredStack.splice(idx + 1); | ||
| 390 | + return structuredStack; | ||
| 391 | + } | ||
| 392 | + | ||
| 393 | + function prepareStackTrace(fn) { | ||
| 394 | + return (error, stackFrames) => { | ||
| 395 | + const frames = filterInternalStackFrames(error, stackFrames); | ||
| 396 | + if (fn) { | ||
| 397 | + return fn(error, frames); | ||
| 398 | + } | ||
| 399 | + frames.push(error); | ||
| 400 | + return frames.reverse().join('\n at '); | ||
| 401 | + }; | ||
| 402 | + } | ||
| 403 | + | ||
| 378 | 404 | function _parseREPLKeyword(keyword, rest) { | |
| 379 | 405 | var cmd = this.commands[keyword]; | |
| 380 | 406 | if (cmd) { | |
@@ -942,8 +968,6 @@ function complete(line, callback) { | |||
| 942 | 968 | } else { | |
| 943 | 969 | const evalExpr = `try { ${expr} } catch (e) {}`; | |
| 944 | 970 | this.eval(evalExpr, this.context, 'repl', (e, obj) => { | |
| 945 | - // if (e) console.log(e); | ||
| 946 | - | ||
| 947 | 971 | if (obj != null) { | |
| 948 | 972 | if (typeof obj === 'object' || typeof obj === 'function') { | |
| 949 | 973 | try { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,19 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + function a() { | ||
| 4 | + b(); | ||
| 5 | + } | ||
| 6 | + | ||
| 7 | + function b() { | ||
| 8 | + c(); | ||
| 9 | + } | ||
| 10 | + | ||
| 11 | + function c() { | ||
| 12 | + d(function() { throw new Error('Whoops!'); }); | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + function d(f) { | ||
| 16 | + f(); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + a(); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,70 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const fixtures = require('../common/fixtures'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const repl = require('repl'); | ||
| 6 | + | ||
| 7 | + | ||
| 8 | + function run({ command, expected }) { | ||
| 9 | + let accum = ''; | ||
| 10 | + | ||
| 11 | + const inputStream = new common.ArrayStream(); | ||
| 12 | + const outputStream = new common.ArrayStream(); | ||
| 13 | + | ||
| 14 | + outputStream.write = (data) => accum += data.replace('\r', ''); | ||
| 15 | + | ||
| 16 | + const r = repl.start({ | ||
| 17 | + prompt: '', | ||
| 18 | + input: inputStream, | ||
| 19 | + output: outputStream, | ||
| 20 | + terminal: false, | ||
| 21 | + useColors: false | ||
| 22 | + }); | ||
| 23 | + | ||
| 24 | + r.write(`${command}\n`); | ||
| 25 | + assert.strictEqual(accum, expected); | ||
| 26 | + r.close(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + const origPrepareStackTrace = Error.prepareStackTrace; | ||
| 30 | + Error.prepareStackTrace = (err, stack) => { | ||
| 31 | + if (err instanceof SyntaxError) | ||
| 32 | + return err.toString(); | ||
| 33 | + stack.push(err); | ||
| 34 | + return stack.reverse().join('--->\n'); | ||
| 35 | + }; | ||
| 36 | + | ||
| 37 | + process.on('uncaughtException', (e) => { | ||
| 38 | + Error.prepareStackTrace = origPrepareStackTrace; | ||
| 39 | + throw e; | ||
| 40 | + }); | ||
| 41 | + | ||
| 42 | + process.on('exit', () => (Error.prepareStackTrace = origPrepareStackTrace)); | ||
| 43 | + | ||
| 44 | + const tests = [ | ||
| 45 | + { | ||
| 46 | + // test .load for a file that throws | ||
| 47 | + command: `.load ${fixtures.path('repl-pretty-stack.js')}`, | ||
| 48 | + expected: 'Error: Whoops!--->\nrepl:9:24--->\nd (repl:12:3)--->\nc ' + | ||
| 49 | + '(repl:9:3)--->\nb (repl:6:3)--->\na (repl:3:3)\n' | ||
| 50 | + }, | ||
| 51 | + { | ||
| 52 | + command: 'let x y;', | ||
| 53 | + expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n' | ||
| 54 | + }, | ||
| 55 | + { | ||
| 56 | + command: 'throw new Error(\'Whoops!\')', | ||
| 57 | + expected: 'Error: Whoops!\n' | ||
| 58 | + }, | ||
| 59 | + { | ||
| 60 | + command: 'foo = bar;', | ||
| 61 | + expected: 'ReferenceError: bar is not defined\n' | ||
| 62 | + }, | ||
| 63 | + // test anonymous IIFE | ||
| 64 | + { | ||
| 65 | + command: '(function() { throw new Error(\'Whoops!\'); })()', | ||
| 66 | + expected: 'Error: Whoops!--->\nrepl:1:21\n' | ||
| 67 | + } | ||
| 68 | + ]; | ||
| 69 | + | ||
| 70 | + tests.forEach(run); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,55 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const fixtures = require('../common/fixtures'); | ||
| 4 | + const assert = require('assert'); | ||
| 5 | + const repl = require('repl'); | ||
| 6 | + | ||
| 7 | + | ||
| 8 | + function run({ command, expected }) { | ||
| 9 | + let accum = ''; | ||
| 10 | + | ||
| 11 | + const inputStream = new common.ArrayStream(); | ||
| 12 | + const outputStream = new common.ArrayStream(); | ||
| 13 | + | ||
| 14 | + outputStream.write = (data) => accum += data.replace('\r', ''); | ||
| 15 | + | ||
| 16 | + const r = repl.start({ | ||
| 17 | + prompt: '', | ||
| 18 | + input: inputStream, | ||
| 19 | + output: outputStream, | ||
| 20 | + terminal: false, | ||
| 21 | + useColors: false | ||
| 22 | + }); | ||
| 23 | + | ||
| 24 | + r.write(`${command}\n`); | ||
| 25 | + assert.strictEqual(accum, expected); | ||
| 26 | + r.close(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + const tests = [ | ||
| 30 | + { | ||
| 31 | + // test .load for a file that throws | ||
| 32 | + command: `.load ${fixtures.path('repl-pretty-stack.js')}`, | ||
| 33 | + expected: 'Error: Whoops!\n at repl:9:24\n at d (repl:12:3)\n ' + | ||
| 34 | + 'at c (repl:9:3)\n at b (repl:6:3)\n at a (repl:3:3)\n' | ||
| 35 | + }, | ||
| 36 | + { | ||
| 37 | + command: 'let x y;', | ||
| 38 | + expected: 'let x y;\n ^\n\nSyntaxError: Unexpected identifier\n\n' | ||
| 39 | + }, | ||
| 40 | + { | ||
| 41 | + command: 'throw new Error(\'Whoops!\')', | ||
| 42 | + expected: 'Error: Whoops!\n' | ||
| 43 | + }, | ||
| 44 | + { | ||
| 45 | + command: 'foo = bar;', | ||
| 46 | + expected: 'ReferenceError: bar is not defined\n' | ||
| 47 | + }, | ||
| 48 | + // test anonymous IIFE | ||
| 49 | + { | ||
| 50 | + command: '(function() { throw new Error(\'Whoops!\'); })()', | ||
| 51 | + expected: 'Error: Whoops!\n at repl:1:21\n' | ||
| 52 | + } | ||
| 53 | + ]; | ||
| 54 | + | ||
| 55 | + tests.forEach(run); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -71,7 +71,7 @@ function clean_up() { | |||
| 71 | 71 | function strict_mode_error_test() { | |
| 72 | 72 | send_expect([ | |
| 73 | 73 | { client: client_unix, send: 'ref = 1', | |
| 74 | - expect: /^ReferenceError:\sref\sis\snot\sdefined\n\s+at\srepl:1:5/ }, | ||
| 74 | + expect: /^ReferenceError:\sref\sis\snot\sdefined\nnode via Unix socket> $/ }, | ||
| 75 | 75 | ]); | |
| 76 | 76 | } | |
| 77 | 77 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments