Skip to content

Log Error#cause when Rollup running via CLI encounters an error #5166

@brawaru

Description

@brawaru

Feature Use Case

class ValueError {
  public readonly code = "MYPLUGIN_ILLEGAL_VALUE_ERROR"
}
class TransformerError {
  public readonly code = "MYPLUGIN_TRANSFORMER_ERROR"
}
class TransformError {
  public readonly code = "MYPLUGIN_TRANSFORM_ERROR"
}
// <logic ...>
function transform(code: string, id: string) {
  try {
    runTransformers(code)
  } catch (err) {
    throw new TransformError('Failed to run transformers', { cause: err })
  }
}
const { generate } = await rollup({ /* ... */ })
const { output } = await generate({ format: 'esm' })

// ⚡ ERROR!
// { code: 'PLUGIN_ERROR', id: 'my-file.coolext', hook: 'transform', pluginCode: 'MYLPGUNI_TRANSFORM_ERROR', plugin: 'my-plugin', watchFiles: [ '.../input.js', '.../my-file.coolext' ] }
//   Caused by:
//     { code: 'MYPLUGIN_TRANSFORM_VALUE_ERROR', message: 'Failed to run transformers' }
//   Caused by:
//     { code: 'MYPLUGIN_TRANSFORMER_ERROR', message: 'Transformer "encode_values" run failed' }
//   Caused by:
//     { code: 'MYPLUGIN_ILLEGAL_VALUE_ERROR', message: 'Illegal value provided at line 5, col 5' }

Feature Proposal

For quite some time JS had a cause option and property on Error objects. This property allows to chain several errors, providing a logical stack of errors, which can help better understand the problem, as well as time developers need to spend figuring out the best place to throw the error.

Unfortunately, unlike Node.js and browsers, Rollup doesn't seem to log causes, and only prints the top level error, which actually might not make any sense whatsoever without being backed by the causes. In the example above, when Rollup is being ran programmatically, it throws an error which causes can be easily logged, however when running Rollup via a CLI, the only message you will see is:

[!] (plugin my-plugin) Error: Failed to run transformers
my-file.coolext
    at Object.transform (...)
    at transform (node_modules\rollup\dist\shared\rollup.js:25604:16)
    at ModuleLoader.addModuleSource (node_modules\rollup\dist\shared\rollup.js:25804:30)

It'd be great if Rollup would print all causes when it fails, like Node.js does:

Error: Troublesome run of the program
    at REPL3:1:13
    at Script.runInThisContext (node:vm:123:12)
    ... 7 lines matching cause stack trace ...
    at [_line] [as _line] (node:internal/readline/interface:893:18) {
  [cause]: Error: Root of all your troubles has caused you problem once again
      at REPL2:1:13
      at Script.runInThisContext (node:vm:123:12)
      ... 7 lines matching cause stack trace ...
      at [_line] [as _line] (node:internal/readline/interface:893:18) {
    [cause]: Error: I am the root of all your troubles mwahaha
        at REPL1:1:8
        at Script.runInThisContext (node:vm:123:12)
        at REPLServer.defaultEval (node:repl:569:29)
        at bound (node:domain:433:15)
        at REPLServer.runBound [as eval] (node:domain:444:12)
        at REPLServer.onLine (node:repl:899:10)
        at REPLServer.emit (node:events:526:35)
        at REPLServer.emit (node:domain:489:12)
        at [_onLine] [as _onLine] (node:internal/readline/interface:422:12)
        at [_line] [as _line] (node:internal/readline/interface:893:18)
  }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions