Replies: 1 comment
-
|
Your second approach is actually the idiomatic way to do this in Effect. The pattern of const program = Effect.tryPromise(() => callPromise()).pipe(
Effect.tapError((error) =>
Effect.logError("callPromise failed", Cause.die(error))
),
Effect.mapError(
(error) =>
new MyCustomError({
message: "My custom error",
cause: error,
})
)
);If you want the original error preserved in structured logging (not just the message), use const program = Effect.tryPromise(() => callPromise()).pipe(
Effect.tapError((error) =>
Effect.logError("callPromise failed").pipe(
Effect.annotateLogs({ originalError: String(error) })
)
),
Effect.mapError(
(error) =>
new MyCustomError({
message: "My custom error",
cause: error,
})
)
);Another option — if you want logging to happen automatically whenever this error type is encountered, you can use // In a shared error handler / layer
const withErrorLogging = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
effect.pipe(
Effect.tapErrorCause((cause) => Effect.logError(cause))
);
The reason you can't run Effect inside the So your second approach is the right one. The only thing I'd suggest is using |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there, I started using Effect in a couple of week, and I'm curious what is the best way to use tryPromise and log the original error.
So what I do is:
And I want to log the error with Effect.logError, but cannot run effect in the catch. The best option I could find is the following, but feel like that's not the best option:
Could someone provide a better option for this? Thank you 🙏
Beta Was this translation helpful? Give feedback.
All reactions