Handling and Tracking Errors in TypeScript with Effect and Sentry

Given something like this:
  const program = await runtime.runPromiseExit(
    Effect.gen(function* () {
      const command = yield* decode(OrderMandatePaymentCreateCommand)({
        orderId: record.id,
      }).pipe(Effect.withSpan("actions/orderMandatePaymentCreate/decode"));

      return yield* orderMandatePaymentCreateHandler(command);
    })
      .pipe(
        Effect.tapError((error) =>
          Effect.gen(function* () {
            const errorTracker = yield* ErrorTracker;
            yield* errorTracker.captureException(error);
          }),
        ),
      )
      .pipe(Effect.withSpan("actions/orderMandatePaymentCreate/run")),
  );

And my ErrorTracker looking like this:
export const ErrorTrackerSentry = Layer.succeed(
  ErrorTracker,
  ErrorTracker.of({
    captureException: (error: Cause.YieldableError) =>
      Effect.gen(function* () {
        const DEFAULT_ERROR_MESSAGE = "ErrorTrackerSentry.captureException";
        yield* Effect.logError(error.message ?? DEFAULT_ERROR_MESSAGE, {
          error: error.toJSON(),
        });
        yield* Effect.try({
          try: () => {
            const scope = Sentry.getCurrentScope();
            Sentry.captureException(error, scope);
          },
          catch: (unknown) =>
            new ErrorTrackerError({
              message: "Sentry exception capture failed",
              cause: errorEnsure(unknown),
            }),
        });
      }),
  }),
);


How can I bind the exception to the parent span?
Currently, my traces are showing as expected, and the issues are being forwarded, but they are distinct.
The captured exception is not bound to the appropriate trace.
Was this page helpful?