Pnpm: Better error handling for pnpmfile.js syntax error

Created on 9 Oct 2017  Â·  3Comments  Â·  Source: pnpm/pnpm

pnpmfile.js

export default {}
$ pnpm

 ERROR  Unexpected token export
at createScript             vm.js:74
at runInThisContext         vm.js:116
at _compile                 module.js:533
at js                       module.js:580
at load                     module.js:503
at tryModuleLoad            module.js:466
at _load                    module.js:458
at require                  module.js:513
at require                  internal/module.js:11
at requireHooks             /Users/Vaughan/dev-live/dotfiler/node_modules/.registry.npmjs.org/pnpm/1.16.0/node_modules/pnpm/src/cmd/install.ts:26
at installCmd [as install]  /Users/Vaughan/dev-live/dotfiler/node_modules/.registry.npmjs.org/pnpm/1.16.0/node_modules/pnpm/src/cmd/install.ts:15
at                          /Users/Vaughan/dev-live/dotfiler/node_modules/.registry.npmjs.org/pnpm/1.16.0/node_modules/pnpm/src/bin/pnpm.ts:123
at next
at fulfilled                /Users/Vaughan/dev-live/dotfiler/node_modules/.registry.npmjs.org/pnpm/1.16.0/node_modules/pnpm/lib/bin/pnpm.js:5
at _tickCallback            internal/process/next_tick.js:169

Should say that there was an issue running the pnpmfile.js.

good first issue in progress

Most helpful comment

@zkochan FYI, I've recently added a proper SyntaxError parsing to the stacktracey module directly.

For example, when trying to require this file (named test_files/syntax_error.js):

// next line contains a syntax error (not a valid JavaScript)
foo->bar ()

...the pretty printed call stack for the error thrown would be:

at (syntax error)                  test_files/syntax_error.js:2  foo->bar ()
at createScript                    vm.js:74
at runInThisContext                vm.js:116
at _compile                        module.js:588
at load                            module.js:545
at tryModuleLoad                   module.js:508
at _load                           module.js:500
at require                         module.js:568
at require                         internal/module.js:11
at it                              test.js:184                   try { require ('./test_files/syntax_error.js') }
at runCallback                     timers.js:781
at tryOnImmediate                  timers.js:743
at processImmediate [as _immediat  timers.js:714

...where the first line is generated from parsing the raw output from the util.inspect call in Node.

But, still, the default printer is more informative, as it highlights the error cause with the ^ symbol:

/Users/mac/stacktracey/test_files/syntax_error.js:2
foo->bar ()
    ^

SyntaxError: Unexpected token >
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)

Actually, I can add some optional ANSI styling to the pretty printed output, highlighting the erroneous characters with the bold flag or something.

Also, this parsing does not work in Node v4, as Node v4 does not provide any meaningful SyntaxError data even with it's default printer. What a shame...

All 3 comments

@zkochan I think this is related to how errors are handled inside the pnpm. Unfortunately, Node.js doesn't allow to extract any meaningful message from SyntaxError instances that are thrown during scripts parsing in require. So any person who implements a custom error reporting should explicitly take care of SyntaxErrors, defaulting output of those to the console.log (which magically is able to display meaningful messages for this kind of errors).

For example, this is how this situation is handled in ololog/reporter (a custom reporter for Mocha):

const printError = e => {

    log.newline ()

    if (e instanceof SyntaxError) {

        console.log (e) // revert to default console.log because it shows source line where a SyntaxError was occured (something we can't do with Ololog now...)

    } else if (('actual' in e) && ('expected' in e)) { // Assertion

        log.bright.red.error ('[AssertionError] ' + e.message)
        log.newline ()
        log.red.error.indent (1) ('actual:  ', e.actual)
        log.newline ()
        log.green.error.indent (1) ('expected:', e.expected)

        log.newline ()
        log.bright.red.error.indent (1) (new StackTracey (e).pretty)

    } else {
        log.bright.red.error (e)
    }    
}

Hope this helps!

@zkochan FYI, I've recently added a proper SyntaxError parsing to the stacktracey module directly.

For example, when trying to require this file (named test_files/syntax_error.js):

// next line contains a syntax error (not a valid JavaScript)
foo->bar ()

...the pretty printed call stack for the error thrown would be:

at (syntax error)                  test_files/syntax_error.js:2  foo->bar ()
at createScript                    vm.js:74
at runInThisContext                vm.js:116
at _compile                        module.js:588
at load                            module.js:545
at tryModuleLoad                   module.js:508
at _load                           module.js:500
at require                         module.js:568
at require                         internal/module.js:11
at it                              test.js:184                   try { require ('./test_files/syntax_error.js') }
at runCallback                     timers.js:781
at tryOnImmediate                  timers.js:743
at processImmediate [as _immediat  timers.js:714

...where the first line is generated from parsing the raw output from the util.inspect call in Node.

But, still, the default printer is more informative, as it highlights the error cause with the ^ symbol:

/Users/mac/stacktracey/test_files/syntax_error.js:2
foo->bar ()
    ^

SyntaxError: Unexpected token >
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)

Actually, I can add some optional ANSI styling to the pretty printed output, highlighting the erroneous characters with the bold flag or something.

Also, this parsing does not work in Node v4, as Node v4 does not provide any meaningful SyntaxError data even with it's default printer. What a shame...

@xpl, thanks for help. For some the latest version of stacktreacey did not print detaied syntax error, so I used console.error(err)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zkochan picture zkochan  Â·  3Comments

andreineculau picture andreineculau  Â·  3Comments

ExE-Boss picture ExE-Boss  Â·  4Comments

Dmitry-N-Medvedev picture Dmitry-N-Medvedev  Â·  4Comments

andreypopp picture andreypopp  Â·  5Comments