From 61bf65ff0d136cb2945062c3c875ac976cbd99ad Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Mon, 22 Jul 2019 16:57:54 +0200 Subject: [PATCH] fix(log): error when logging object with circular refs --- garden-service/src/logger/log-entry.ts | 6 +++--- garden-service/src/logger/renderers.ts | 5 ++--- garden-service/src/logger/util.ts | 8 ++++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/garden-service/src/logger/log-entry.ts b/garden-service/src/logger/log-entry.ts index e320056032..109f3980c6 100644 --- a/garden-service/src/logger/log-entry.ts +++ b/garden-service/src/logger/log-entry.ts @@ -11,7 +11,7 @@ import * as nodeEmoji from "node-emoji" import { flatten } from "lodash" import { LogNode, LogLevel } from "./log-node" -import { getChildEntries, findParentEntry } from "./util" +import { getChildEntries, findParentEntry, sanitizeObject } from "./util" import { GardenError } from "../exceptions" import { Omit } from "../util/util" import { Logger } from "./logger" @@ -196,11 +196,11 @@ export class LogEntry extends LogNode { } inspect() { - console.log(JSON.stringify({ + console.log(JSON.stringify(sanitizeObject({ ...this.opts, level: this.level, children: this.children, - })) + }))) } filterBySection(section: string): LogEntry[] { diff --git a/garden-service/src/logger/renderers.ts b/garden-service/src/logger/renderers.ts index 8e8787c9b4..92ffb2d921 100644 --- a/garden-service/src/logger/renderers.ts +++ b/garden-service/src/logger/renderers.ts @@ -10,7 +10,6 @@ import * as logSymbols from "log-symbols" import * as yaml from "js-yaml" import chalk from "chalk" import stripAnsi from "strip-ansi" -import * as CircularJSON from "circular-json" import { curryRight, flow, @@ -26,7 +25,7 @@ import { LogEntry } from "./log-entry" import { JsonLogEntry } from "./writers/json-terminal-writer" import { highlightYaml, deepFilter } from "../util/util" import { isNumber } from "util" -import { printEmoji } from "./util" +import { printEmoji, sanitizeObject } from "./util" export type ToRender = string | ((...args: any[]) => string) export type Renderer = [ToRender, any[]] | ToRender[] @@ -91,7 +90,7 @@ export function renderError(entry: LogEntry) { if (!isEmpty(filteredDetail)) { try { - const sanitized = JSON.parse(CircularJSON.stringify(filteredDetail)) + const sanitized = sanitizeObject(filteredDetail) const yamlDetail = yaml.safeDump(sanitized, { noRefs: true, skipInvalid: true }) out += `\nError Details:\n${yamlDetail}` } catch (err) { diff --git a/garden-service/src/logger/util.ts b/garden-service/src/logger/util.ts index 83b363410a..1fbb02b921 100644 --- a/garden-service/src/logger/util.ts +++ b/garden-service/src/logger/util.ts @@ -8,6 +8,7 @@ import * as nodeEmoji from "node-emoji" import chalk from "chalk" +import * as CircularJSON from "circular-json" import { LogNode } from "./log-node" import { LogEntry, CreateOpts, EmojiName } from "./log-entry" @@ -137,3 +138,10 @@ export function printFooter(log: LogEntry) { export function printWarningMessage(log: LogEntry, text: string) { return log.info({ emoji: "warning", msg: chalk.bold.yellow(text) }) } + +/** + * Strips undefined values and circular references from an object. + */ +export function sanitizeObject(obj: any) { + return JSON.parse(CircularJSON.stringify(obj)) +}