Skip to content

Commit

Permalink
fix(cli): edge-case error when writing error logs
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Feb 24, 2020
1 parent 5145d3f commit 29fe3aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions garden-service/src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import hasAnsi = require("has-ansi")
import { LogEntry, MessageState } from "./log-entry"
import { JsonLogEntry } from "./writers/json-terminal-writer"
import { highlightYaml, deepFilter, PickFromUnion } from "../util/util"
import { isNumber } from "util"
import { isNumber, isBuffer } from "util"
import { printEmoji, sanitizeObject } from "./util"
import { LoggerType, Logger } from "./logger"

Expand Down Expand Up @@ -91,8 +91,8 @@ export function renderError(entry: LogEntry) {
let out = stack || message

// We recursively filter out internal fields (i.e. having names starting with _).
const filteredDetail = deepFilter(detail, (_, key: string | number) => {
return isNumber(key) || !key.startsWith("_")
const filteredDetail = deepFilter(detail, (_: any, key: string | number) => {
return !isNumber(key) && !key.startsWith("_")
})

if (!isEmpty(filteredDetail)) {
Expand Down
5 changes: 5 additions & 0 deletions garden-service/src/logger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import chalk from "chalk"
import CircularJSON from "circular-json"
import { LogNode } from "./log-node"
import { LogEntry, LogEntryParams, EmojiName } from "./log-entry"
import { isBuffer } from "util"
import { deepMap } from "../util/util"

export interface Node {
children: any[]
Expand Down Expand Up @@ -140,5 +142,8 @@ export function printWarningMessage(log: LogEntry, text: string) {
* Strips undefined values and circular references from an object.
*/
export function sanitizeObject(obj: any) {
obj = deepMap(obj, (value: any) => {
return isBuffer(value) ? "<Buffer>" : value
})
return JSON.parse(CircularJSON.stringify(obj))
}
13 changes: 12 additions & 1 deletion garden-service/test/unit/src/logger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { expect } from "chai"

import { getChildNodes } from "../../../../src/logger/util"
import { getChildNodes, sanitizeObject } from "../../../../src/logger/util"

describe("util", () => {
describe("getChildNodes", () => {
Expand Down Expand Up @@ -41,4 +41,15 @@ describe("util", () => {
expect(nodeList.map((n) => n.id)).to.eql([1, 2, 3, 4, 5, 6])
})
})

describe("sanitizeObject", () => {
it("should replace any buffer values", () => {
const obj = {
something: "test",
buffer: Buffer.from("bla"),
}

expect(sanitizeObject(obj)).to.eql({ something: "test", buffer: "<Buffer>" })
})
})
})

0 comments on commit 29fe3aa

Please sign in to comment.