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 error cause in Diagnostics Panel #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "nodejs-testing",
"displayName": "node:test runner",
"description": "Test integration for node:test native tests",
"publisher": "connor4312",
"name": "nodejs-testing-with-error-cause",
"displayName": "node:test runner with error cause",
"description": "Test integration for node:test native tests (with error cause)",
"publisher": "drk-mtr (all good work done by connor4312)",
"version": "1.5.1",
"engines": {
"vscode": "^1.88.0"
Expand Down
22 changes: 21 additions & 1 deletion src/runner-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,33 @@ for (const channel of ["stderr", "stdout"] as const) {
},
});
}
type ErrorNested = Error & { cause: Error | any };

const appendNewLineIfSome = (str: string | null | undefined) => str ? `${str}\r\n` : '';

const buildStackPhrase = (msg: string, error: ErrorNested, errorDepth = 0) => {
const prefix = msg + '\r\n';
const message = errorDepth == 0 ? '' : appendNewLineIfSome((error as any).message);
const stack = appendNewLineIfSome((error as any).stack);
return prefix + message + stack;
}

const maxErrorDepth = 5;

const buildErrorString = (error: ErrorNested, msg: string = '', errorDepth: number = 0): string =>
error.cause && errorDepth < maxErrorDepth - 1
? buildErrorString(error.cause, buildStackPhrase(msg, error, errorDepth), errorDepth + 1)
: buildStackPhrase(msg, error);

module.exports = async function* reporter(source: AsyncGenerator<TestEvent>) {
for await (const evt of source) {
if (evt.type === "test:fail") {
const err = evt.data.details.error as Error & { cause?: any };
if (err.cause instanceof Error) {
(err.cause as any)._message = err.cause.message;
(err.cause as any)._message =
buildErrorString(
err.cause as ErrorNested,
err.cause.message + '\r\n');
(err.cause as any)._stack = err.stack ? parse(err.stack) : undefined;
}

Expand Down