{"version":3,"file":"stack.mjs","sources":["../../src/pool-diagnostics/stack.ts"],"sourcesContent":["/** Lowest `Error.stackTraceLimit` used for the one re-capture below. Node's default is 10. */\nconst DEEP_STACK_LIMIT = 50;\n\n/**\n * knex creates the timeout error with Node's default of 10 frames, which on the query path ends\n * inside the document service or a lifecycle hook. The caller of this function is awaited by the\n * same async chain, so capture that chain again with a higher limit (only on this failure path) and\n * keep knex's header and first frame. The error object itself is unchanged.\n *\n * A deeper stack is a convenience and must never replace the error being reported, so this leaves\n * the stack alone when stacks are disabled or when another library (APM, source maps) installed a\n * `prepareStackTrace` that returns something other than a string.\n */\nexport const deepenStack = (error: Error): void => {\n  if (typeof error.stack !== 'string' || !(Error.stackTraceLimit > 0)) {\n    return;\n  }\n\n  try {\n    const previousLimit = Error.stackTraceLimit;\n    Error.stackTraceLimit = Math.max(previousLimit, DEEP_STACK_LIMIT);\n\n    let capture: unknown;\n    try {\n      capture = new Error().stack;\n    } finally {\n      Error.stackTraceLimit = previousLimit;\n    }\n\n    if (typeof capture !== 'string') {\n      return;\n    }\n\n    const lines = error.stack.split('\\n');\n    const firstFrame = lines.findIndex((line) => line.trimStart().startsWith('at '));\n    if (firstFrame === -1) {\n      return;\n    }\n\n    // Header plus the frame where knex created the error, then the re-captured chain without its\n    // \"Error\" line and this function's own frame\n    error.stack = [...lines.slice(0, firstFrame + 1), ...capture.split('\\n').slice(2)].join('\\n');\n  } catch {\n    // Keep knex's stack: a deeper stack is a convenience and must never replace the error\n  }\n};\n"],"names":["DEEP_STACK_LIMIT","deepenStack","error","stack","Error","stackTraceLimit","previousLimit","Math","max","capture","lines","split","firstFrame","findIndex","line","trimStart","startsWith","slice","join"],"mappings":"AAAA,+FACA,MAAMA,gBAAAA,GAAmB,EAAA;AAEzB;;;;;;;;;IAUO,MAAMC,WAAAA,GAAc,CAACC,KAAAA,GAAAA;IAC1B,IAAI,OAAOA,KAAAA,CAAMC,KAAK,KAAK,QAAA,IAAY,EAAEC,KAAAA,CAAM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