Skip to content

Commit

Permalink
fix(log): error when logging object with circular refs
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Jul 23, 2019
1 parent 46fb474 commit 61bf65f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
6 changes: 3 additions & 3 deletions garden-service/src/logger/log-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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[] {
Expand Down
5 changes: 2 additions & 3 deletions garden-service/src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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[]
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions garden-service/src/logger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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))
}

0 comments on commit 61bf65f

Please sign in to comment.