Skip to content

Commit

Permalink
Carry both call-site and definition site in Effect.fn, auto-trace to …
Browse files Browse the repository at this point in the history
…anon
  • Loading branch information
mikearnaldi committed Dec 17, 2024
1 parent 03f73c6 commit 0d32de7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-pears-remember.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Carry both call-site and definition site in Effect.fn, auto-trace to anon
29 changes: 26 additions & 3 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10130,9 +10130,31 @@ export const fn:
name: string,
options?: Tracer.SpanOptions
) => fn.Gen & fn.NonGen) = function(nameOrBody: Function | string, ...pipeables: Array<any>) {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 2
const error0 = new Error()
Error.stackTraceLimit = limit
if (typeof nameOrBody !== "string") {
return function(this: any) {
return fnApply(this, nameOrBody, arguments as any, pipeables)
return function(this: any, ...args: Array<any>) {
const limit = Error.stackTraceLimit
Error.stackTraceLimit = 2
const error = new Error()
Error.stackTraceLimit = limit
let cache: false | string = false
const captureStackTrace = () => {
if (cache !== false) {
return cache
}
if (error.stack) {
const stack0 = error0.stack!.trim().split("\n")
const stack = error.stack.trim().split("\n")
cache = `${stack0.slice(2).join("\n").trim()}\n${stack.slice(2).join("\n").trim()}`
return cache
}
}
const effect = fnApply(this, nameOrBody, args, pipeables)
const opts: any = { captureStackTrace }
return withSpan(effect, "<anonymous>", opts)
} as any
}
const name = nameOrBody
Expand All @@ -10149,8 +10171,9 @@ export const fn:
return cache
}
if (error.stack) {
const stack0 = error0.stack!.trim().split("\n")
const stack = error.stack.trim().split("\n")
cache = stack.slice(2).join("\n").trim()
cache = `${stack0.slice(2).join("\n").trim()}\n${stack.slice(2).join("\n").trim()}`
return cache
}
}
Expand Down
10 changes: 6 additions & 4 deletions packages/effect/src/internal/cause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ export const prettyErrorMessage = (u: unknown): string => {
return stringifyCircular(u)
}

const locationRegex = /\((.*)\)/
const locationRegex = /\((.*)\)/g

/** @internal */
export const spanToTrace = globalValue("effect/Tracer/spanToTrace", () => new WeakMap())
Expand Down Expand Up @@ -1105,9 +1105,11 @@ const prettyErrorStack = (message: string, stack: string, span?: Span | undefine
if (typeof stackFn === "function") {
const stack = stackFn()
if (typeof stack === "string") {
const locationMatch = stack.match(locationRegex)
const location = locationMatch ? locationMatch[1] : stack.replace(/^at /, "")
out.push(` at ${current.name} (${location})`)
const locationMatchAll = stack.matchAll(locationRegex)
for (const locationMatch of locationMatchAll) {
const location = locationMatch ? locationMatch[1] : stack.replace(/^at /, "")
out.push(` at ${current.name} (${location})`)
}
} else {
out.push(` at ${current.name}`)
}
Expand Down

0 comments on commit 0d32de7

Please sign in to comment.