Skip to content
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
2 changes: 1 addition & 1 deletion .craft.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
minVersion: "2.33.0"
minVersion: "3.5.0"
workspaces:
packages/cli:
changelog:
Expand Down
11 changes: 7 additions & 4 deletions packages/logger/src/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function formatError(error: ErrorLike, depth: number, stack: boolean): string {
let cause = "";
if (isErrorLike(error.cause)) {
cause = `${stack ? "\n\n" : " "}${formatError(error.cause, depth + 1, stack)}`;
} else if (error.cause) {
} else if ("cause" in error) {
cause = `${stack ? `\n\n${" ".repeat(depth + 1)}` : " "}[cause]: ${formatWithOptions(INSPECT_OPTIONS, error.cause)}`;
}
if (!stack) {
Expand All @@ -57,8 +57,11 @@ export function formatMessage(
args: readonly unknown[],
{ stack }: { stack: boolean },
): string {
return formatWithOptions(
INSPECT_OPTIONS,
...args.map((arg) => (isErrorLike(arg) ? formatError(arg, 0, stack) : arg)),
const formatted = args.map((arg) =>
isErrorLike(arg) ? formatError(arg, 0, stack) : arg,
);
// An error is not a format string, so `%s` in its message stays literal.
return isErrorLike(args[0])
? formatWithOptions(INSPECT_OPTIONS, "%s", ...formatted)
: formatWithOptions(INSPECT_OPTIONS, ...formatted);
}
27 changes: 27 additions & 0 deletions packages/logger/test/logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,31 @@ describe("sentryReporter", () => {
"Upload failed: Request failed [cause]: socket hang up",
);
});

test("keeps causes that are falsy", () => {
const { calls, sentryLogger } = fakeSentryLogger();
const logger = createLogger({ reporters: [sentryReporter(sentryLogger)] });
for (const cause of [0, "", false, null, undefined]) {
logger.error(new Error("Failed", { cause }));
}
logger.error(new Error("No cause"));
expect(calls.map(([, message]) => message)).toEqual([
"Failed [cause]: 0",
"Failed [cause]: ",
"Failed [cause]: false",
"Failed [cause]: null",
"Failed [cause]: undefined",
"No cause",
]);
});

test("does not read an error's message as a format string", () => {
const { calls, sentryLogger } = fakeSentryLogger();
createLogger({ reporters: [sentryReporter(sentryLogger)] }).error(
new Error("Bad value %s"),
"extra",
{ id: 1 },
);
expect(calls[0]?.[1]).toBe("Bad value %s extra { id: 1 }");
});
});
Loading