test-action-debian-package/node_modules/pino-std-serializers/lib/err.js
Dawid Dziurla 9308795b8b
update
2020-03-26 15:37:35 +01:00

65 lines
1.3 KiB
JavaScript

'use strict'
module.exports = errSerializer
const seen = Symbol('circular-ref-tag')
const rawSymbol = Symbol('pino-raw-err-ref')
const pinoErrProto = Object.create({}, {
type: {
enumerable: true,
writable: true,
value: undefined
},
message: {
enumerable: true,
writable: true,
value: undefined
},
stack: {
enumerable: true,
writable: true,
value: undefined
},
raw: {
enumerable: false,
get: function () {
return this[rawSymbol]
},
set: function (val) {
this[rawSymbol] = val
}
}
})
Object.defineProperty(pinoErrProto, rawSymbol, {
writable: true,
value: {}
})
function errSerializer (err) {
if (!(err instanceof Error)) {
return err
}
err[seen] = undefined // tag to prevent re-looking at this
const _err = Object.create(pinoErrProto)
_err.type = err.constructor.name
_err.message = err.message
_err.stack = err.stack
for (const key in err) {
if (_err[key] === undefined) {
const val = err[key]
if (val instanceof Error) {
if (!val.hasOwnProperty(seen)) {
_err[key] = errSerializer(val)
}
} else {
_err[key] = val
}
}
}
delete err[seen] // clean up tag in case err is serialized again later
_err.raw = err
return _err
}