fix: print helpful message when internal CLI error happens#5344
fix: print helpful message when internal CLI error happens#5344mark-wiemer merged 1 commit intomochajs:mainfrom
Conversation
|
|
|
Switching to draft pending #5078 (comment). We can't review without a reproduction, and it might be that all buggy behavior was already fixed? Thanks for the PR in the interim though! |
For clarity, this is not about fixing whatever that triggered the error, but about improving the logged diagnostics information when an error (unexpectedly) happens. While the specific trigger was fixed, it is still possible to end up in this case if some other internal error occurs. If you want to reproduce locally, you could throw an error from Line 377 in abf3dd9 There are probably other ways to trigger this issue, but I haven't looked. If you are concerned about logging too much information (when an error message is already present), I could add a check to only log the error when the message is null. |
There was a problem hiding this comment.
Pull Request Overview
This PR enhances error reporting in the CLI by printing the original error object to stderr when an internal error occurs, making the “ERROR: null” message more informative.
- Prints the caught error object after showing help and the generic error message.
- Exits with status code 1 after logging the full error.
Comments suppressed due to low confidence (1)
lib/cli/cli.js:64
- Add a test case that simulates an internal CLI error to verify that the original error message (or stack) is printed as expected.
console.error(err);
JoshuaKGoldberg
left a comment
There was a problem hiding this comment.
@Rob--W #5078 is open and I see that you were right all along 🙂. I think this PR is ready for un-drafting - and pretty close to ready for merge. WDYT about the suggested change? And, mind if we un-draft this?
Normally I'd request test coverage but for an edge case like this that "shouldn't" ever happen, I think it's reasonable to skip testing.
There have been issues where the reported error is "ERROR: null", which is very unhelpful. Here is an analysis of a specific example: mochajs#5048 (comment) Although the trigger for that error was fixed by mochajs#5074, the unhelpfulness of "ERROR: null" was not addressed. To help with debugging, this patch prints the original error when this stage is unexpectedly reached.
7cda07e to
9f1fad1
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5344 +/- ##
==========================================
- Coverage 93.52% 93.50% -0.02%
==========================================
Files 57 57
Lines 4465 4467 +2
Branches 918 919 +1
==========================================
+ Hits 4176 4177 +1
- Misses 289 290 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hey @Rob--W, could you add a new unit test for this? Something that would've failed on main but passes with your changes? |
|
@mark-wiemer Hey if you take another look at the PR I have linked in the comments (#5562) You can see that this is happening on internal Mocha Error. Can you explain why exactly in this very niche issue (mostly for people to report/fix mocha issue) a test would be required? |
|
Hey @Lightning00Blade, I know this is a pretty niche issue from what we can see, but it's hard to know just how often it occurs to end-users with wonky setups. Mocha specifically had a huge userbase a decade ago, and many folks using it today have patched together build pipelines over the years that are surprisingly weird. So even if an edge case is extremely rare, we want a unit test to cover it. That way, if we need to change behavior later, we can better guarantee that we didn't break any "promises" we made with previous fixes. In short, we test everything because it's best practice :) |
|
Do you have pointers / examples of preferred practices to use for testing this? I did not find something close when I looked for existing ones at https://github.com/mochajs/mocha/tree/main/test/node-unit/cli To test this effectively, we would have to inspect the serialized output of the cli execution. The next best alternative is to spy on the console global to detect console log calls. I'm leaning towards the latter so that I can simply check the raw error instead of having to interpret Node's interpretation of the error serialization. |
|
Hey @JoshuaKGoldberg, can you assist here? I don't have capacity right now :/ |
|
Yeah, this is a tricky one. Normally I'd suggest extracting the code that includes logging into a function in a separate file, and then unit test that function. But this specific area of code is pretty inter-tangled with the yargs stuff. This is also a single-line change that is easy to verify manually. So I feel a little more comfortable suggesting: how about we merge as-is, and file a followup task for adding test coverage? @mark-wiemer |
|
Sounds good. I'll manually verify later today, merge this if it works, and open a chore to cover this scenario with automated tests |
PR Checklist
ERROR: nullpart of it)status: accepting prsOverview
There have been issues where the reported error is "ERROR: null", which is very unhelpful. Here is an analysis of a specific example: #5048 (comment)
Although the trigger for that error was fixed by #5074, the unhelpfulness of "ERROR: null" was not addressed.
To help with debugging, this patch prints the original error when this stage is unexpectedly reached.