Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

889 B
Raw Blame
import { sql } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../database";

export type TaskReadDatabase = Parameters<
  Parameters<typeof db.transaction>[0]
>[0];

export async function boundedTaskRead<T>(
  read: (tx: TaskReadDatabase) => Promise<T>,
  message = "Description request took too long; retry later",
) {
  try {
    return await db.transaction(
      async (tx) => {
        await tx.execute(sql`set local statement_timeout = '3s'`);
        return read(tx);
      },
      { isolationLevel: "repeatable read", accessMode: "read only" },
    );
  } catch (error) {
    const cause = error instanceof Error ? (error.cause ?? error) : error;
    if (
      cause &&
      typeof cause === "object" &&
      "code" in cause &&
      cause.code === "57014"
    )
      throw new HTTPException(503, { message });
    throw error;
  }
}