Effect CommunityEC
Effect Community3y ago
78 replies
sandromaglione

Alternative to Effect.tryCatchPromise to avoid re-throwing

Hi everyone,

I have an API that instead of throwing returns an object containing
data
and error, if error is not null, then the request failed.
I used Effect.tryCatchPromise to implement this:

Effect.tryCatchPromise(
  async () => {
    const { error, ...response } = await client
      .from("table")
      .select();
      
    if (error !== null) {
      throw error;
    }

    return response;
  },
  (error) => new QueryError(error)
)


Is there some other Effect constructor that allows to avoid throwing? Something like:

Effect.someConstructor(
  async () => {
    const { error, ...response } = await client
      .from("table")
      .select();
      
    if (error !== null) {
      return Effect.fail(error);
    }

    return Effect.succeed(response);
  },
)
Was this page helpful?