Skip to content

Instantly share code, notes, and snippets.

@astur astur/log.js
Created May 25, 2018

Embed
What would you like to do?
Why node.js Error objects sucks?..
~ $ node
> const err = new Error('TEST')
undefined
> JSON.stringify(err)
'{}'
> Object.keys(err)
[]
> Reflect.ownKeys(err) //where is a 'name'?
[ 'stack', 'message' ]
> for(let k in err) console.log(k)
undefined
> err.field = 'bla'
'bla'
> console.log(err) //do you see this 'bla' at the end?...
{ Error: TEST
at repl:1:13
at ContextifyScript.Script.runInThisContext (vm.js:50:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:440:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
at REPLServer.Interface._onLine (readline.js:282:10)
at REPLServer.Interface._line (readline.js:631:8) field: 'bla' }
undefined
> Object.keys(err)
[ 'field' ]
> err.obj = {a: {b: {c: {d: {e: [1,2,3]}}}}}
{ a: { b: { c: [Object] } } }
> console.log(err) // OMG!
{ Error: TEST
at repl:1:13
at ContextifyScript.Script.runInThisContext (vm.js:50:33)
at REPLServer.defaultEval (repl.js:239:29)
at bound (domain.js:301:14)
at REPLServer.runBound [as eval] (domain.js:314:12)
at REPLServer.onLine (repl.js:440:10)
at emitOne (events.js:120:20)
at REPLServer.emit (events.js:210:7)
at REPLServer.Interface._onLine (readline.js:282:10)
at REPLServer.Interface._line (readline.js:631:8) field: 'bla', obj: { a: { b: [Object] } } }
undefined
> JSON.stringify(err) // OMG 2!
'{"field":"bla","obj":{"a":{"b":{"c":{"d":{"e":[1,2,3]}}}}}}'
> process.versions
{ http_parser: '2.7.0',
node: '8.6.0',
v8: '6.0.287.53',
uv: '1.14.1',
zlib: '1.2.11',
ares: '1.10.1-DEV',
modules: '57',
nghttp2: '1.25.0',
openssl: '1.0.2l',
icu: '59.1',
unicode: '9.0',
cldr: '31.0.1',
tz: '2017b' }
>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.