Empty `cause` in QueryError when logging in frontend

Hey all,

Interesting issue.

I have this code:

const mapToQueryError = (cause: unknown) => new QueryError({ cause })

const mapToQueryErrorDebug = (cause: unknown) => {
  const error = new QueryError({ cause })

  console.log(util.inspect(error, true, 5, true))

  return error
}

export const insertIntoUsersTable = (
  users: UserInsert | UserInsert[],
) => DrizzleService.pipe(
  Effect.andThen(({ db }) => Effect.tryPromise({
    try: () => db.insert(tables.users).values(Array.ensure(users)).returning(),
    catch: mapToQueryError,
  },
  )))


export default defineEventHandler(async (event) => {
  const pipeline = pipe(
    parseBody(event),
    Effect.flatMap(decode),
    Effect.tap(user => pipe(
      findUserByEmail(user.email),
      Effect.map(Option.map(() => Effect.fail(createAuthError()))),
      Effect.flatMap(Option.getOrElse(() => Effect.succeed('Ok'))),
    )),
    Effect.flatMap(input => TenantService.pipe(
      Effect.flatMap(({ getTenant }) => getTenant()),
      Effect.map(id => Record.set(input, 'orgId', id)),
    )),
    Effect.flatMap(user => insertIntoUsersTable(user)),
  )

  return await pipe(
    pipeline,
    provideTenantServiceFor(event),
    provideDrizzleService(),
    run('register'),
  )
})


I get this object when logging in the frontend:

timestamp=2024-12-15T19:45:15.670Z level=INFO fiber=#3 message="{
  \"_id\": \"Exit\",
  \"_tag\": \"Failure\",
  \"cause\": {
    \"_id\": \"Cause\",
    \"_tag\": \"Fail\",
    \"failure\": {
      \"cause\": {},
      \"_tag\": \"QueryError\"
    }
  }
}"


For some reason, cause is empty, while it should be populated. Am I missing something? I tried some debugging, but that didn't seem to help.

E.G:

const mapToQueryErrorDebug = (cause: unknown) => { 
  console.error('mTQED', typeof cause, 'iof E:', cause instanceof Error, cause);
  return new QueryError({ cause })
}

Results in:

ERROR  mTQED object iof E: true D1_ERROR: FOREIGN KEY constraint failed: SQLITE_CONSTRAINT

  at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
  at async D1PreparedQuery.all (node_modules\.pnpm\drizzle-orm@0.38.1_@cloudflare+workers-types@4.20241205.0\node_modules\src\d1\session.ts:194:16)


Any help would be appreciated!
Was this page helpful?