fix(log): fix serializing BigInt value in object (#3550)

This commit is contained in:
Mizuki 2023-08-21 13:26:58 +09:00 committed by GitHub
parent 01d45f1748
commit fd5722bbb6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 7 deletions

View File

@ -123,8 +123,9 @@ export class Logger {
return msg instanceof Function ? fnResult : msg;
}
asString(data: unknown): string {
asString(data: unknown, isProperty = false): string {
if (typeof data === "string") {
if (isProperty) return `"${data}"`;
return data;
} else if (
data === null ||
@ -138,7 +139,11 @@ export class Logger {
} else if (data instanceof Error) {
return data.stack!;
} else if (typeof data === "object") {
return JSON.stringify(data);
return `{${
Object.entries(data)
.map(([k, v]) => `"${k}":${this.asString(v, true)}`)
.join(",")
}}`;
}
return "undefined";
}

View File

@ -241,15 +241,33 @@ Deno.test(
payload: "data",
other: 123,
});
const data19: { payload: string; other: bigint } = logger.error({
payload: "data",
other: 123n,
});
assertEquals(data19, {
payload: "data",
other: 123n,
});
const data20: { payload: string; other: bigint } = logger.error(
{ payload: "data", other: 123n },
1,
);
assertEquals(data20, {
payload: "data",
other: 123n,
});
assertEquals(handler.messages[16], 'ERROR {"payload":"data","other":123}');
assertEquals(handler.messages[17], 'ERROR {"payload":"data","other":123}');
assertEquals(handler.messages[18], 'ERROR {"payload":"data","other":123}');
assertEquals(handler.messages[19], 'ERROR {"payload":"data","other":123}');
// error
const error = new RangeError("Uh-oh!");
const data19: RangeError = logger.error(error);
assertEquals(data19, error);
const messages19 = handler.messages[18].split("\n");
assertEquals(messages19[0], `ERROR ${error.name}: ${error.message}`);
assertMatch(messages19[1], /^\s+at file:.*\d+:\d+$/);
const data21: RangeError = logger.error(error);
assertEquals(data21, error);
const messages21 = handler.messages[20].split("\n");
assertEquals(messages21[0], `ERROR ${error.name}: ${error.message}`);
assertMatch(messages21[1], /^\s+at file:.*\d+:\d+$/);
},
);