Skip to content

Commit bf3ac25

Browse files
committed
improvement(routes): log the deepest cause's stack outside production
1 parent 250181b commit bf3ac25

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎apps/sim/lib/core/utils/with-route-handler.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createLogger, runWithRequestContext } from '@sim/logger'
2-
import { describeError, getErrorMessage, redactBoundParameters } from '@sim/utils/errors'
2+
import { describeError, findCause, getErrorMessage, redactBoundParameters } from '@sim/utils/errors'
33
import type { NextRequest } from 'next/server'
44
import { NextResponse } from 'next/server'
55
import { getRateLimitHeaders } from '@/lib/api/server/rate-limit-context'
@@ -93,12 +93,20 @@ function traceIdFromTraceparent(header: string | null | undefined): string | und
9393
* message names the query. The shared describer reads the deepest cause and
9494
* strips bound parameter values, so user data never reaches the log.
9595
*/
96-
function errorDetail(error: unknown): { cause?: string; code?: string } {
96+
function errorDetail(error: unknown): { cause?: string; code?: string; causeStack?: string } {
9797
if (!(error instanceof Error) || error.cause === undefined) return {}
9898
const described = describeError(error)
99+
/** Outside production the deepest cause's stack says where a wrapped failure was raised. */
100+
const deepest = findCause(
101+
error,
102+
(candidate): candidate is Error => candidate instanceof Error && candidate.cause === undefined
103+
)
99104
return {
100105
cause: `${described.name}: ${described.message}`,
101106
...(described.code ? { code: described.code } : {}),
107+
...(process.env.NODE_ENV !== 'production' && deepest?.stack
108+
? { causeStack: redactBoundParameters(deepest.stack) }
109+
: {}),
102110
}
103111
}
104112

0 commit comments

Comments
 (0)