Seeking Help with Logging Errors in tryPromise Catch Statement

Hey team, I feel like I am doing something fundamentally wrong here. What I am trying to do is simply Effect.logError inside the tryPromise catch statement:

export const insertUser = (userInput: SignupTouserInput) =>
  Effect.gen(function* (_) {
    const { db, schema, connection } = yield* _(DbSeedContextService);
    //
    yield* Effect.logInfo("Inserting to User", { userInput });

    return yield* _(Effect.tryPromise({
      try: () =>
        db
          .insert(schema.User)
          .values({
            email: userInput.email,
            name: userInput.name,
            id: uuid(),
          })
          .execute(),
      catch: (err) => Effect.gen(function* () {
        const pgErr = err as { code: string };
        if (pgErr.code === "23505") {
          yield* _(Effect.logError("User row already exists", userInput, err));
          return new DAOError("User row already exists", err, "UNIQUE_CONSTRAINT");
        }
        yield* _(Effect.logError("Error inserting to User", userInput, err));
        return new DAOError("Unknown DAO error", err, "UNKNOWN_ERROR");
      }),
    }));
  });


What is happening is that

- return new DAOError("User row already exists", err, "UNIQUE_CONSTRAINT"); is returning an Effect to my Effect

So that when I run my insertUser Effect, I cannot catchAll and recognize the code
Property 'code' does not exist on type 'Effect<DAOError, never, never>'.ts(2339)


Previously I tried to log an error directly in my .catch without using a Gen function.

catch: (err) => {
    Effect.logError(err);
   return new DaoError(...)
}

// THere is also the option to use runSync - But this seems wrong?

Effect.runSync(Effect.logError("User row already exists", userInput, err));

I think I am doing or thinking about this in the wrong way.

What is the cleanest approach to log inside Effect?
Was this page helpful?