Trouble Understanding Function with tryPromise and Decode in TypeScript

Hello,
I struggle to understand something with a function where I use tryPromise + some decode function.

Here is the snippet
// ...
  merchantByApiToken: (customerApiToken) =>
    Effect.tryPromise({
      try: async () => await heyMantleClient.getCustomer(customerApiToken),
      catch: (unknown) => new MantleError({ unknown }),
    }).pipe(
      Effect.flatMap((res) =>
        S.decode(Customer)({
          id: res.id,
          subscription: res.subscription
            ? {
                id: res.subscription.id,
                plan: {
                  id: res.subscription.plan.id,
                  features: res.subscription.plan.features,
                },
              }
            : null,
        })
      )
    ),
// ...

I get an inferred type of:
(property) MantleClient.merchantByApiToken: (apiToken: string) => Effect.Effect<Customer, MantleError | ParseError, never>

Which is basically what I want.

However when there's a parsing error in the decode function, it does not seem handled by the Effect error channel and instead it's throwing a low-level error and makes the process crash.

This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().

When using decode within a flatmap in other pipelines I don't think I ever encountered such issue, so I assume I am doing something wrong with the tryPromise maybe, but I cannot seem to find what's wrong
Was this page helpful?