Skip to content

Commit d2ae718

Browse files
committed
feat: error object formatting
1 parent 70866fd commit d2ae718

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/transports/console.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ export class ConsoleTransport implements Transport {
107107
return JSON.stringify(part)
108108
}
109109

110+
// Special handling for Error objects - include stack trace
111+
if (part instanceof Error) {
112+
return `${part.toString()}${part.stack ? `\nstack: ${part.stack}` : ''}`
113+
}
114+
110115
// Check if object has a non-default toString method
111116
if (part.toString && part.toString !== Object.prototype.toString) {
112117
return part.toString()

test/console-transport.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,22 @@ describe("ConsoleTransport", () => {
293293
const [formattedMsg] = consoleLogSpy.mock.calls[0];
294294
expect(formattedMsg).toContain(`Regular object: ${JSON.stringify(regularObj)}`);
295295
});
296+
297+
test("formats Error objects with name and message", () => {
298+
const error = new Error("Something went wrong");
299+
error.name = "CustomError";
300+
const entry: LogEntry = {
301+
timestamp: new Date(),
302+
level: LogLevel.ERROR,
303+
message: "",
304+
messageParts: ["Error object:", error]
305+
};
306+
307+
transport.write(entry);
308+
309+
const [formattedMsg] = consoleLogSpy.mock.calls[0];
310+
expect(formattedMsg).toContain("Error object:");
311+
expect(formattedMsg).toContain("CustomError: Something went wrong");
312+
expect(formattedMsg).toContain("stack:");
313+
});
296314
});

0 commit comments

Comments
 (0)