Extracting Nested Errors in `effect/Cause` with `@effect/sql-drizzle` and `@effect/sql`

Hey everyone! I'm unsure which channel to post this in as it is mostly concering effect/Cause. I'm digging into how Cause nests failures when using @effect/sql-drizzle + @effect/sql.

The minimal repro lives in the repo I'm working on.

packages/_internal/db-admin/src/execute.ts shows the brute-force version. The only way I can reach the underlying postgres.PostgresError inside the SqlError cause is to JSON round-trip the DrizzleQueryError – without that the nested FiberFailure keeps the Postgres error hidden behind non-enumerable symbols.

pipe(error.cause, JSON.stringify, JSON.parse, (parsed) => {
  if (Cause.isFailType(parsed.cause.cause)) {
    const pgError = new postgres.PostgresError(parsed.cause.cause.failure.cause)
    // ...
  }
});


Am I missing an existing helper for exposing the original PostgresError (or even just the innermost failure) from a SqlError? Ideally I'd like to map a failed query to a DbError without the serialization trick or having to manually walk every property.

At the end of the day I would like to create a robust DbError class with a static method called match or something that could take an unknown input and do some Cause / pattern matching magic to return a new DbError instance with as much error information as possible and wouldn't mind some tips/trick's as to some clean & ergonomic ways to accomplish this as I have yet to come to a satisfactory solution:
class DbError extends Data.TaggedError("DbError")<{
  readonly type: keyof typeof PostgresErrorEnum | "UNKNOWN";
  readonly message: string;
  readonly pgError: postgres.PostgresError | null;
  readonly drizzleQueryError: DrizzleQueryError | null;
  readonly sqlError: SqlError | null;
  readonly cause: unknown;
}> {}


Any pointers or examples would be super appreciated.
Was this page helpful?