Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show all cause in Error #5422

Merged
merged 6 commits into from Apr 3, 2024
Merged

feat: show all cause in Error #5422

merged 6 commits into from Apr 3, 2024

Conversation

devohda
Copy link
Contributor

@devohda devohda commented Mar 9, 2024

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers: #5166

Description

In ES2022+, Error has cause field optionally. (type is unknown)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/cause

So if cause is in Error then show all cause log in cli.

Copy link

vercel bot commented Mar 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 3, 2024 5:11am

Copy link

codecov bot commented Mar 11, 2024

Codecov Report

Attention: Patch coverage is 90.00000% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 98.79%. Comparing base (1dd3c02) to head (bb8a247).
Report is 2 commits behind head on master.

Files Patch % Lines
cli/logging.ts 90.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5422      +/-   ##
==========================================
- Coverage   98.80%   98.79%   -0.01%     
==========================================
  Files         237      237              
  Lines        9432     9445      +13     
  Branches     2398     2403       +5     
==========================================
+ Hits         9319     9331      +12     
  Misses         48       48              
- Partials       65       66       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, that looks already very good! Just two things:

  • This looks very similar to how Node would print such an error. Node, however, would indent every subsequent cause by two more spaces (i.e. two for the first cause, four for the second etc.). Personally I find this helps visually so that it does not look like one huge stack trace. Maybe we want to add this as well? (one easy way to indent is to .split('\n').map(line => indent+line).join('\n'))
  • It would be nice to test this output somewhat. Of course we should not test stack traces directly as they contain paths on a user's machine, but we can test stuff in between. Here is how you would do it:

If you do not know how to run tests, look at CONTRIBUTING.md.
Tests for command line output are located in test/cli. Create a new folder in test/cli/samples with the following files:

  • main.js: It does not matter a lot, but we need an entry point. E.g. console.log('ok') would be some content that does not produce a warning.
  • rollup.config.mjs: We define a config that always throws an error with nested errors, e.g.
    export default {
      input: "main.js",
      plugins: [
        {
          buildStart() {
            throw new Error("Outer error", {
              cause: new Error("Inner error", {
                cause: new Error("Innermost error")
              })
             });
          }
        }
      ]
    };
  • _config.js: The test configuration. This would be the typical contents
    const { assertIncludes } = require('../../../utils.js');
    
    module.exports = defineTest({
    	description: 'prints error cause',
    	command: 'rollup --config rollup.config.mjs',
    	// We expect an error and want to make assertions about the output
    	error: () => true,
    	stderr: stderr => {
    		// We just assert the parts of the output that do not change
    		assertIncludes(stderr, '\n[!] (plugin at position 1) Error: Outer error\n    at ');
    		assertIncludes(stderr, '\n  [cause] Error: Inner error\n      at ');
    		assertIncludes(stderr, '\n    [cause] Error: Innermost error\n        at ');
    	}
    });
    If you add solo: true, you can just run this single test, just do not forget to remove this line before committing.

@lukastaegert lukastaegert merged commit 5301dec into rollup:master Apr 3, 2024
19 of 20 checks passed
Copy link

github-actions bot commented Apr 3, 2024

This PR has been released as part of rollup@4.14.0. You can test it via npm install rollup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants