-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(serializers): Implemented "error with cause" serializer (#130)
- Loading branch information
1 parent
b4edb5d
commit 0ccfa26
Showing
9 changed files
with
389 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
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 | ||
}, | ||
aggregateErrors: { | ||
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: {} | ||
}) | ||
|
||
module.exports = { | ||
pinoErrProto, | ||
pinoErrorSymbols: { | ||
seen, | ||
rawSymbol | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
module.exports = errWithCauseSerializer | ||
|
||
const { isErrorLike } = require('./err-helpers') | ||
const { pinoErrProto, pinoErrorSymbols } = require('./err-proto') | ||
const { seen } = pinoErrorSymbols | ||
|
||
const { toString } = Object.prototype | ||
|
||
function errWithCauseSerializer (err) { | ||
if (!isErrorLike(err)) { | ||
return err | ||
} | ||
|
||
err[seen] = undefined // tag to prevent re-looking at this | ||
const _err = Object.create(pinoErrProto) | ||
_err.type = toString.call(err.constructor) === '[object Function]' | ||
? err.constructor.name | ||
: err.name | ||
_err.message = err.message | ||
_err.stack = err.stack | ||
|
||
if (Array.isArray(err.errors)) { | ||
_err.aggregateErrors = err.errors.map(err => errWithCauseSerializer(err)) | ||
} | ||
|
||
if (isErrorLike(err.cause) && !Object.prototype.hasOwnProperty.call(err.cause, seen)) { | ||
_err.cause = errWithCauseSerializer(err.cause) | ||
} | ||
|
||
for (const key in err) { | ||
if (_err[key] === undefined) { | ||
const val = err[key] | ||
if (isErrorLike(val)) { | ||
if (!Object.prototype.hasOwnProperty.call(val, seen)) { | ||
_err[key] = errWithCauseSerializer(val) | ||
} | ||
} else { | ||
_err[key] = val | ||
} | ||
} | ||
} | ||
|
||
delete err[seen] // clean up tag in case err is serialized again later | ||
_err.raw = err | ||
return _err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.