Skip to content

Commit 02df892

Browse files
fix(logger): Keep falsy causes and literal % in error messages (#1779)
This fixes the Warden findings that were posted on #1773 after it merged. - **Falsy causes:** `formatError` showed a cause only when it was truthy, so `new Error("x", { cause: 0 })` printed just `x`. It now shows the cause whenever the error has one (`0`, `""`, `false`, `null`, `undefined`), as `util.inspect` does. - **An error as the first argument:** `formatMessage` turned errors into strings before calling `util.format`, so an error passed first became the format string. `logger.info(new Error("Failed for %s"), "extra")` printed `Failed for extra`. The `%s` now stays literal and the remaining arguments are added after it, as `console.log` does. - **Craft `minVersion`:** raised from 2.33.0 to 3.5.0. The logger workspace uses `{{workspaceName}}` and `{{packageSimpleName}}`, and Craft only resolves those from 3.5.0 onward. The release workflow's `getsentry/craft@v3` already installs 3.5.0. Warden said the placeholders are left unresolved, which is wrong for the release workflow, but an older local Craft would have left them as literal text without any error. This raises the minimum for every workspace in this repository. Tests cover each falsy cause and an error message containing `%s`. For other output, the build is byte-identical to `main` across about 24,600 formatted records. <!-- junior-request-attribution:start --> via @BYK. <!-- junior-request-attribution:end --> <!-- junior-session-footer:start --> <!-- junior-conversation-id:local%3Aweb%3A052a28796a4c37b4b8b5f916 --> -- [View Session in Sentry](https://sentry.sentry.io/explore/conversations/local%3Aweb%3A052a28796a4c37b4b8b5f916/?project=4510944073809921) <!-- junior-session-footer:end --> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com> Co-authored-by: BYK <126780+BYK@users.noreply.github.com>
1 parent 34c1ceb commit 02df892

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

‎.craft.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
minVersion: "2.33.0"
1+
minVersion: "3.5.0"
22
workspaces:
33
packages/cli:
44
changelog:

‎packages/logger/src/message.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function formatError(error: ErrorLike, depth: number, stack: boolean): string {
3131
let cause = "";
3232
if (isErrorLike(error.cause)) {
3333
cause = `${stack ? "\n\n" : " "}${formatError(error.cause, depth + 1, stack)}`;
34-
} else if (error.cause) {
34+
} else if ("cause" in error) {
3535
cause = `${stack ? `\n\n${" ".repeat(depth + 1)}` : " "}[cause]: ${formatWithOptions(INSPECT_OPTIONS, error.cause)}`;
3636
}
3737
if (!stack) {
@@ -57,8 +57,11 @@ export function formatMessage(
5757
args: readonly unknown[],
5858
{ stack }: { stack: boolean },
5959
): string {
60-
return formatWithOptions(
61-
INSPECT_OPTIONS,
62-
...args.map((arg) => (isErrorLike(arg) ? formatError(arg, 0, stack) : arg)),
60+
const formatted = args.map((arg) =>
61+
isErrorLike(arg) ? formatError(arg, 0, stack) : arg,
6362
);
63+
// An error is not a format string, so `%s` in its message stays literal.
64+
return isErrorLike(args[0])
65+
? formatWithOptions(INSPECT_OPTIONS, "%s", ...formatted)
66+
: formatWithOptions(INSPECT_OPTIONS, ...formatted);
6467
}

‎packages/logger/test/logger.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,31 @@ describe("sentryReporter", () => {
468468
"Upload failed: Request failed [cause]: socket hang up",
469469
);
470470
});
471+
472+
test("keeps causes that are falsy", () => {
473+
const { calls, sentryLogger } = fakeSentryLogger();
474+
const logger = createLogger({ reporters: [sentryReporter(sentryLogger)] });
475+
for (const cause of [0, "", false, null, undefined]) {
476+
logger.error(new Error("Failed", { cause }));
477+
}
478+
logger.error(new Error("No cause"));
479+
expect(calls.map(([, message]) => message)).toEqual([
480+
"Failed [cause]: 0",
481+
"Failed [cause]: ",
482+
"Failed [cause]: false",
483+
"Failed [cause]: null",
484+
"Failed [cause]: undefined",
485+
"No cause",
486+
]);
487+
});
488+
489+
test("does not read an error's message as a format string", () => {
490+
const { calls, sentryLogger } = fakeSentryLogger();
491+
createLogger({ reporters: [sentryReporter(sentryLogger)] }).error(
492+
new Error("Bad value %s"),
493+
"extra",
494+
{ id: 1 },
495+
);
496+
expect(calls[0]?.[1]).toBe("Bad value %s extra { id: 1 }");
497+
});
471498
});

0 commit comments

Comments
 (0)