Skip to content

Commit

Permalink
fix: log errors
Browse files Browse the repository at this point in the history
  • Loading branch information
izatop committed Sep 14, 2021
1 parent 67f7b14 commit 0bc07db
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/util/src/Logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,17 @@ export class Logger {
log.args = [];
for (const arg of args) {
if (isInstanceOf(arg, Error)) {
const error: Record<any, any> = {error: {message: arg.message, stack: arg.stack}};
const error: Record<any, any> = {message: arg.message, stack: arg.stack};
if (isLogable(arg)) {
const logValue = arg.getLogValue();
if (isDefined(logValue)) {
error.extra = arg.getLogValue();
}
}

log.args.push(error);
const {constructor: {name}} = arg;

log.args.push({error: {name, ...error}});

continue;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/util/test/src/logger/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ describe("Logger", () => {
});

test("should log logable error", () => {
const value = {test: "value"};
const extra = {test: "value"};
const logger = new Logger("Test");
const error = new AssertionError("Error", {value});
const error = new AssertionError("Error", extra);
error.stack = "Error stack";
logger.error("Error", error);

const {message, stack, constructor: {name}} = error;
const {args = []} = logs.pop() ?? {args: []};
expect(args.pop()).toEqual({value});
expect(args.pop()).toBe(error.stack);
expect(args.pop()).toEqual({error: {name, message, stack, extra}});
});
});

0 comments on commit 0bc07db

Please sign in to comment.