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: support report error cause #308

Merged
merged 3 commits into from
Dec 19, 2024
Merged
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
13 changes: 13 additions & 0 deletions examples/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { consola } from "./utils";

const error = new Error("This is an error", {
cause: new Error("This is the cause", {
cause: new Error("This is the cause of the cause"),
}),
});

Check warning on line 7 in examples/error.ts

View check run for this annotation

Codecov / codecov/patch

examples/error.ts#L3-L7

Added lines #L3 - L7 were not covered by tests

console.error(error);

Check warning on line 9 in examples/error.ts

View check run for this annotation

Codecov / codecov/patch

examples/error.ts#L9

Added line #L9 was not covered by tests

console.log("\n");

Check warning on line 11 in examples/error.ts

View check run for this annotation

Codecov / codecov/patch

examples/error.ts#L11

Added line #L11 was not covered by tests

consola.error(error);

Check warning on line 13 in examples/error.ts

View check run for this annotation

Codecov / codecov/patch

examples/error.ts#L13

Added line #L13 was not covered by tests
18 changes: 16 additions & 2 deletions src/reporters/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,27 @@

export class BasicReporter implements ConsolaReporter {
formatStack(stack: string, opts: FormatOptions) {
return " " + parseStack(stack).join("\n ");
const indent = " ".repeat((opts?.errorLevel || 0) + 1);
return indent + parseStack(stack).join(`\n${indent}`);
}

Check warning on line 17 in src/reporters/basic.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/basic.ts#L15-L17

Added lines #L15 - L17 were not covered by tests

formatError(err: any, opts: FormatOptions): string {
const message = err.message ?? formatWithOptions(opts, err);
const stack = err.stack ? this.formatStack(err.stack, opts) : "";

Check warning on line 21 in src/reporters/basic.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/basic.ts#L20-L21

Added lines #L20 - L21 were not covered by tests

const level = opts?.errorLevel || 0;
const causedPrefix = level > 0 ? `${" ".repeat(level)}[cause]: ` : "";
const causedError = err.cause
? "\n\n" + this.formatError(err.cause, { ...opts, errorLevel: level + 1 })
: "";

Check warning on line 27 in src/reporters/basic.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/basic.ts#L23-L27

Added lines #L23 - L27 were not covered by tests

return causedPrefix + message + "\n" + stack + causedError;

Check warning on line 29 in src/reporters/basic.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/basic.ts#L29

Added line #L29 was not covered by tests
}

formatArgs(args: any[], opts: FormatOptions) {
const _args = args.map((arg) => {
if (arg && typeof arg.stack === "string") {
return arg.message + "\n" + this.formatStack(arg.stack, opts);
return this.formatError(arg, opts);

Check warning on line 35 in src/reporters/basic.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/basic.ts#L35

Added line #L35 was not covered by tests
}
return arg;
});
Expand Down
7 changes: 4 additions & 3 deletions src/reporters/fancy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@
}

export class FancyReporter extends BasicReporter {
formatStack(stack: string) {
formatStack(stack: string, opts: FormatOptions) {
const indent = " ".repeat((opts?.errorLevel || 0) + 1);

Check warning on line 50 in src/reporters/fancy.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/fancy.ts#L50

Added line #L50 was not covered by tests
return (
"\n" +
`\n${indent}` +

Check warning on line 52 in src/reporters/fancy.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/fancy.ts#L52

Added line #L52 was not covered by tests
parseStack(stack)
.map(
(line) =>
Expand All @@ -57,7 +58,7 @@
.replace(/^at +/, (m) => colors.gray(m))
.replace(/\((.+)\)/, (_, m) => `(${colors.cyan(m)})`),
)
.join("\n")
.join(`\n${indent}`)

Check warning on line 61 in src/reporters/fancy.ts

View check run for this annotation

Codecov / codecov/patch

src/reporters/fancy.ts#L61

Added line #L61 was not covered by tests
);
}

Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export interface FormatOptions {
*/
compact?: boolean | number;

/**
* Error cause level.
*/
errorLevel?: number;

/**
* Allows additional custom formatting options.
*/
Expand Down
Loading