@@ -13,6 +13,7 @@ const defaultLogger = {
1313let enabled = false
1414let logger = defaultLogger
1515let logChannel = new LogChannel ( )
16+ let stackTraceLimitFunction = onError
1617
1718function withNoop ( fn ) {
1819 const store = storage ( 'legacy' ) . getStore ( )
@@ -61,12 +62,28 @@ function getErrorLog (err) {
6162 }
6263}
6364
65+ function setStackTraceLimitFunction ( fn ) {
66+ if ( typeof fn !== 'function' ) {
67+ throw new TypeError ( 'stackTraceLimitFunction must be a function' )
68+ }
69+ stackTraceLimitFunction = fn
70+ }
71+
6472function onError ( err ) {
6573 const { formatted, cause } = getErrorLog ( err )
6674
6775 // calling twice logger.error() because Error cause is only available in nodejs v16.9.0
6876 // TODO: replace it with Error(message, { cause }) when cause has broad support
69- if ( formatted ) withNoop ( ( ) => logger . error ( new Error ( formatted ) ) )
77+ if ( formatted ) {
78+ withNoop ( ( ) => {
79+ const l = Error . stackTraceLimit
80+ Error . stackTraceLimit = 0
81+ const e = new Error ( formatted )
82+ Error . stackTraceLimit = l
83+ Error . captureStackTrace ( e , stackTraceLimitFunction )
84+ logger . error ( e )
85+ } )
86+ }
7087 if ( cause ) withNoop ( ( ) => logger . error ( cause ) )
7188}
7289
@@ -122,4 +139,4 @@ function trace (...args) {
122139 onTrace ( Log . parse ( ...args ) )
123140}
124141
125- module . exports = { use, toggle, reset, error, warn, info, debug, trace }
142+ module . exports = { use, toggle, reset, error, warn, info, debug, trace, setStackTraceLimitFunction }
0 commit comments