Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { Effect, Struct, pipe } from 'effect'; |
| 2 | |
| 3 | import { stripCwdPath } from 'effect-errors/logic/path'; |
| 4 | import { stackAtRegex } from 'effect-errors/logic/stack'; |
| 5 | |
| 6 | import type { CaptureErrorsOptions } from '../capture-errors'; |
| 7 | import type { PrettyError } from '../types/pretty-error.type'; |
| 8 | import { getSourcesFromStack } from './get-sources-from-stack'; |
| 9 | |
| 10 | const dropTag = <T extends { readonly _tag: string }>(value: T) => Struct.omit(value, ['_tag']); |
| 11 | |
| 12 | export const transformRawError = |
| 13 | ({ stripCwd }: CaptureErrorsOptions) => |
| 14 | ({ message, stack: maybeStack, spans, errorType, isPlainString }: PrettyError) => |
| 15 | pipe( |
| 16 | Effect.gen(function* () { |
| 17 | // v4 has no `Span` object on a `Cause` to source-map (see |
| 18 | // `logic/errors/span-annotation.ts`), so `sources`/`location` can only |
| 19 | // ever come from the raw Node stack, not from span attributes anymore. |
| 20 | const { sources, location } = yield* getSourcesFromStack(maybeStack); |
| 21 | |
| 22 | let stack: string | undefined; |
| 23 | if (maybeStack !== undefined) { |
| 24 | stack = stripCwd === true ? stripCwdPath(maybeStack) : maybeStack; |
| 25 | } |
| 26 | |
| 27 | return { |
| 28 | errorType, |
| 29 | message, |
| 30 | stack: stack?.replaceAll(stackAtRegex, 'at ').split('\r\n'), |
| 31 | // `Struct.omit` returns a plain object, dropping the `Data` prototype |
| 32 | // along with the tag: these values leave the source-map layer here. |
| 33 | sources: sources.length > 0 ? sources.map(dropTag) : undefined, |
| 34 | location: location.length > 0 ? location.map(dropTag) : undefined, |
| 35 | spans, |
| 36 | isPlainString, |
| 37 | }; |
| 38 | }), |
| 39 | Effect.withSpan('transform-raw-error') |
| 40 | ); |