Skip to content

armgit / fuobe

Public Imported from GitHub
0

The open creative studio on ComfyUI: images, video, 3D and audio on open weights, on your own GPU or the cloud. Camera motions, effects, apps, Cinema, Layers, Image to 3D and an AI agent.

Accounts 2 fixes: refusals pass through the studios' and routes' own error answers

Clauded9357e207a21
diff --git a/src/app/api/ads/batches/route.ts b/src/app/api/ads/batches/route.ts
index eb813721..c40546c1 100644
--- a/src/app/api/ads/batches/route.ts
+++ b/src/app/api/ads/batches/route.ts
@@ -1,6 +1,7 @@
import { after } from "next/server";
import { BatchBody, createBatch } from "@/server/ads";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** "Create N ads": creatives land in the grid at once; copy and pictures follow in the background. */
@@ -12,6 +13,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
after(() => run);
return Response.json({ batchId, creatives }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/ads/creatives/[id]/retry/route.ts b/src/app/api/ads/creatives/[id]/retry/route.ts
index b52875b0..0c8023d0 100644
--- a/src/app/api/ads/creatives/[id]/retry/route.ts
+++ b/src/app/api/ads/creatives/[id]/retry/route.ts
@@ -1,5 +1,6 @@
import { retryCreative } from "@/server/ads";
import { failure } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Make a failed ad again (this spends credits again). */
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (_req: Request, ctx:
try {
return Response.json(await retryCreative(id), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return (e as Error).message === "Not found" ? Response.json({ error: "Not found" }, { status: 404 }) : failure(e);
}
});
diff --git a/src/app/api/ads/creatives/[id]/save/route.ts b/src/app/api/ads/creatives/[id]/save/route.ts
index 02edb761..715546d0 100644
--- a/src/app/api/ads/creatives/[id]/save/route.ts
+++ b/src/app/api/ads/creatives/[id]/save/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { saveComposite } from "@/server/ads";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ url: z.string().startsWith("/api/files/"), ratio: z.enum(["1:1", "4:5", "9:16"]) });
@@ -13,6 +14,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(saveComposite(id, body), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return (e as Error).message === "Not found" ? Response.json({ error: "Not found" }, { status: 404 }) : failure(e);
}
});
diff --git a/src/app/api/ads/estimate/route.ts b/src/app/api/ads/estimate/route.ts
index 28c66577..75fb685c 100644
--- a/src/app/api/ads/estimate/route.ts
+++ b/src/app/api/ads/estimate/route.ts
@@ -1,5 +1,6 @@
import { BatchBody, estimateBatch } from "@/server/ads";
import { readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** The total before the run: ads × price per ad (+ the copy), and which model renders them. */
@@ -9,6 +10,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(estimateBatch(body));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ costUsd: null, total: 0, perAd: null, demo: false, blocker: (e as Error).message });
}
});
diff --git a/src/app/api/ads/multiplier/estimate/route.ts b/src/app/api/ads/multiplier/estimate/route.ts
index dc1586c6..1fe5cba9 100644
--- a/src/app/api/ads/multiplier/estimate/route.ts
+++ b/src/app/api/ads/multiplier/estimate/route.ts
@@ -1,4 +1,5 @@
import { estimateMultiplier, MultiplierBody, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const POST = handler({ access: "signed-in" }, async (request: Request) => {
@@ -7,6 +8,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await estimateMultiplier(body));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ costUsd: null, demo: false, steps: [], notes: [], runs: 0, blocker: (e as Error).message });
}
});
diff --git a/src/app/api/ads/multiplier/route.ts b/src/app/api/ads/multiplier/route.ts
index 7bf897de..124379fb 100644
--- a/src/app/api/ads/multiplier/route.ts
+++ b/src/app/api/ads/multiplier/route.ts
@@ -1,4 +1,5 @@
import { failure, MultiplierBody, readBody, runMultiplier } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Ad Multiplier: new versions of a reference ad (the whole ad from a prompt, or a new hook / CTA around it). */
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json({ runs: await runMultiplier(body) }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/assist/[id]/approval/route.ts b/src/app/api/assist/[id]/approval/route.ts
index 0311922d..00506bbc 100644
--- a/src/app/api/assist/[id]/approval/route.ts
+++ b/src/app/api/assist/[id]/approval/route.ts
@@ -1,6 +1,7 @@
import { after } from "next/server";
import { z } from "zod";
import { answerApproval, AssistBusyError, run } from "@/server/assist/agent";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ approve: z.boolean() });
@@ -16,6 +17,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
after(() => reply);
return Response.json(thread);
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof AssistBusyError ? 409 : 404 });
}
});
diff --git a/src/app/api/assist/[id]/messages/route.ts b/src/app/api/assist/[id]/messages/route.ts
index d445a545..0359613e 100644
--- a/src/app/api/assist/[id]/messages/route.ts
+++ b/src/app/api/assist/[id]/messages/route.ts
@@ -1,6 +1,7 @@
import { after } from "next/server";
import { addMessage, AssistBusyError, run } from "@/server/assist/agent";
import { AssistMessage } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const POST = handler({ access: "signed-in" }, async (request: Request, ctx: RouteContext<"/api/assist/[id]/messages">) => {
@@ -13,6 +14,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
after(() => reply);
return Response.json(thread);
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof AssistBusyError ? 409 : 404 });
}
});
diff --git a/src/app/api/assist/[id]/route.ts b/src/app/api/assist/[id]/route.ts
index e306caba..89be83ef 100644
--- a/src/app/api/assist/[id]/route.ts
+++ b/src/app/api/assist/[id]/route.ts
@@ -2,6 +2,7 @@ import { connection } from "next/server";
import { z } from "zod";
import { AssistBusyError, deleteThread, getThread, updateThread } from "@/server/assist/agent";
import { ModeChoice } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({
@@ -25,6 +26,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const t = await updateThread((await ctx.params).id, parsed.data);
return t ? Response.json(t) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof AssistBusyError ? 409 : 400 });
}
});
diff --git a/src/app/api/assist/connectors/[connectorId]/route.ts b/src/app/api/assist/connectors/[connectorId]/route.ts
index 7189d784..b9b75c36 100644
--- a/src/app/api/assist/connectors/[connectorId]/route.ts
+++ b/src/app/api/assist/connectors/[connectorId]/route.ts
@@ -1,5 +1,6 @@
import { ConnectorError, deleteConnector, updateConnector } from "@/server/assist/connectors";
import { ConnectorBody } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Rename, change the URL, replace the token ("" removes it; leave it out to keep it), switch on or off. */
@@ -10,6 +11,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const c = updateConnector((await ctx.params).connectorId, parsed.data);
return c ? Response.json(c) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ConnectorError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/connectors/route.ts b/src/app/api/assist/connectors/route.ts
index af3715b5..782aaff2 100644
--- a/src/app/api/assist/connectors/route.ts
+++ b/src/app/api/assist/connectors/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { ConnectorError, createConnector, listConnectors } from "@/server/assist/connectors";
import { ConnectorBody } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Custom MCP connectors, tokens masked. */
@@ -15,6 +16,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createConnector(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ConnectorError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/memory/[memoryId]/route.ts b/src/app/api/assist/memory/[memoryId]/route.ts
index 73426a7d..14f443b8 100644
--- a/src/app/api/assist/memory/[memoryId]/route.ts
+++ b/src/app/api/assist/memory/[memoryId]/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { deleteMemory, MemoryError, updateMemory } from "@/server/assist/memory";
import { MemoryCategorySchema } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({ text: z.string().trim().min(1).max(500).optional(), category: MemoryCategorySchema.optional() });
@@ -11,6 +12,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
try {
return Response.json(updateMemory((await ctx.params).memoryId, parsed.data));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof MemoryError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/memory/import/route.ts b/src/app/api/assist/memory/import/route.ts
index e019a601..3f3dfe93 100644
--- a/src/app/api/assist/memory/import/route.ts
+++ b/src/app/api/assist/memory/import/route.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { parseMemoryImport } from "@/lib/assist";
import { importMemories, MemoryError } from "@/server/assist/memory";
import { MemoryCategorySchema } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -22,6 +23,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(importMemories(items, parsed.data.scope, parsed.data.scope === "project" ? (parsed.data.projectId ?? null) : null));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof MemoryError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/memory/route.ts b/src/app/api/assist/memory/route.ts
index 78544f4a..2cc9c403 100644
--- a/src/app/api/assist/memory/route.ts
+++ b/src/app/api/assist/memory/route.ts
@@ -2,6 +2,7 @@ import { connection } from "next/server";
import { z } from "zod";
import { addMemory, listMemories, MemoryError } from "@/server/assist/memory";
import { MemoryCategorySchema } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Add = z.object({
@@ -28,6 +29,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(addMemory({ ...parsed.data, source: "user" }), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof MemoryError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/projects/[projectId]/route.ts b/src/app/api/assist/projects/[projectId]/route.ts
index b4c63974..47b05647 100644
--- a/src/app/api/assist/projects/[projectId]/route.ts
+++ b/src/app/api/assist/projects/[projectId]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { deleteProject, getProject, ProjectError, updateProject } from "@/server/assist/projects";
import { ProjectBody } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const GET = handler({ access: "signed-in" }, async (_req: Request, ctx: RouteContext<"/api/assist/projects/[projectId]">) => {
@@ -15,6 +16,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
try {
return Response.json(updateProject((await ctx.params).projectId, parsed.data));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ProjectError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/projects/[projectId]/sources/[sourceId]/refresh/route.ts b/src/app/api/assist/projects/[projectId]/sources/[sourceId]/refresh/route.ts
index f9331bfa..ef380048 100644
--- a/src/app/api/assist/projects/[projectId]/sources/[sourceId]/refresh/route.ts
+++ b/src/app/api/assist/projects/[projectId]/sources/[sourceId]/refresh/route.ts
@@ -1,4 +1,5 @@
import { ProjectError, refreshSource } from "@/server/assist/projects";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Fetch a link source again. */
@@ -7,6 +8,7 @@ export const POST = handler({ access: "signed-in" }, async (_req: Request, ctx:
try {
return Response.json(await refreshSource(projectId, sourceId));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ProjectError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/projects/[projectId]/sources/route.ts b/src/app/api/assist/projects/[projectId]/sources/route.ts
index f3d4a2a7..9ef86a26 100644
--- a/src/app/api/assist/projects/[projectId]/sources/route.ts
+++ b/src/app/api/assist/projects/[projectId]/sources/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { addSource, ProjectError } from "@/server/assist/projects";
import { contentTypeFor, deleteAsset, saveFile } from "@/server/storage";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const MAX_BYTES = 25 * 1024 * 1024;
@@ -46,6 +47,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
if (!parsed.success) return Response.json({ error: "Add a link, a file, an element or a note" }, { status: 400 });
return Response.json(await addSource(projectId, parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ProjectError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/projects/route.ts b/src/app/api/assist/projects/route.ts
index b1086b02..7d509551 100644
--- a/src/app/api/assist/projects/route.ts
+++ b/src/app/api/assist/projects/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { createProject, listProjects, ProjectError } from "@/server/assist/projects";
import { ProjectBody } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const GET = handler({ access: "signed-in" }, async () => {
@@ -14,6 +15,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createProject(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ProjectError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/route.ts b/src/app/api/assist/route.ts
index ed9494fb..50475918 100644
--- a/src/app/api/assist/route.ts
+++ b/src/app/api/assist/route.ts
@@ -2,6 +2,7 @@ import { after, connection } from "next/server";
import { z } from "zod";
import { addMessage, availability, createThread, listThreads, resolveAgent, resolveMode, run } from "@/server/assist/agent";
import { AssistMessage, ModeChoice } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Create = AssistMessage.extend({
@@ -31,6 +32,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
thread = createThread({ engine, autoApprove, agent: r.agent, projectId: projectId ?? null, mode: r.mode, model: r.model, effort: r.effort });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 404 });
}
const updated = addMessage(thread.id, text, attachments);
diff --git a/src/app/api/assist/schedules/[scheduleId]/route.ts b/src/app/api/assist/schedules/[scheduleId]/route.ts
index fbcabb7a..3f3380e4 100644
--- a/src/app/api/assist/schedules/[scheduleId]/route.ts
+++ b/src/app/api/assist/schedules/[scheduleId]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { deleteSchedule, getSchedule, ScheduleError, updateSchedule } from "@/server/assist/schedules";
import { ScheduleBody } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = ScheduleBody.partial();
@@ -19,6 +20,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const s = updateSchedule((await ctx.params).scheduleId, parsed.data);
return s ? Response.json(s) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ScheduleError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/schedules/[scheduleId]/run/route.ts b/src/app/api/assist/schedules/[scheduleId]/run/route.ts
index 31dd18ce..eb3e300f 100644
--- a/src/app/api/assist/schedules/[scheduleId]/run/route.ts
+++ b/src/app/api/assist/schedules/[scheduleId]/run/route.ts
@@ -1,5 +1,6 @@
import { after } from "next/server";
import { ScheduleError, startSchedule } from "@/server/assist/schedules";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -14,6 +15,7 @@ export const POST = handler({ access: "signed-in" }, async (_req: Request, ctx:
after(() => started.done);
return Response.json({ threadId: started.threadId }, { status: 202 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ScheduleError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/schedules/route.ts b/src/app/api/assist/schedules/route.ts
index b3bb5ac9..26da4ce4 100644
--- a/src/app/api/assist/schedules/route.ts
+++ b/src/app/api/assist/schedules/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { createSchedule, listSchedules, ScheduleError } from "@/server/assist/schedules";
import { ScheduleBody } from "@/server/assist/schemas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const issue = (e: { issues: { message: string }[] }) => e.issues[0]?.message ?? "Check the schedule";
@@ -18,6 +19,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createSchedule(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof ScheduleError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/skills/[name]/duplicate/route.ts b/src/app/api/assist/skills/[name]/duplicate/route.ts
index f3bbad3a..d583af10 100644
--- a/src/app/api/assist/skills/[name]/duplicate/route.ts
+++ b/src/app/api/assist/skills/[name]/duplicate/route.ts
@@ -1,4 +1,5 @@
import { duplicateSkill, SkillError } from "@/server/skills";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Copy a skill into My skills (the way to change a built-in). */
@@ -6,6 +7,7 @@ export const POST = handler({ access: "signed-in" }, async (_req: Request, ctx:
try {
return Response.json(duplicateSkill((await ctx.params).name), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof SkillError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/skills/[name]/estimate/route.ts b/src/app/api/assist/skills/[name]/estimate/route.ts
index 017c38c4..eb3051d7 100644
--- a/src/app/api/assist/skills/[name]/estimate/route.ts
+++ b/src/app/api/assist/skills/[name]/estimate/route.ts
@@ -1,5 +1,6 @@
import { badBody, recipeFailure, RunBody } from "@/server/recipe-routes";
import { estimateRecipe } from "@/server/recipes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Price a run of a skill's recipe, or many (`rows` or `csv`, priced together with a total). Starts nothing. */
@@ -10,6 +11,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
const { engine, ...input } = parsed.data;
return Response.json(estimateRecipe((await ctx.params).name, input, engine));
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/[name]/recipe/route.ts b/src/app/api/assist/skills/[name]/recipe/route.ts
index 048ae00d..55df898e 100644
--- a/src/app/api/assist/skills/[name]/recipe/route.ts
+++ b/src/app/api/assist/skills/[name]/recipe/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { recipeFailure } from "@/server/recipe-routes";
import { recipeView, saveRecipe } from "@/server/recipes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** A skill's recipe with where it runs and what was checked (engines, ready, needs a re-check). */
@@ -9,6 +10,7 @@ export const GET = handler({ access: "signed-in" }, async (_req: Request, ctx: R
try {
return Response.json(recipeView((await ctx.params).name));
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
@@ -22,6 +24,7 @@ export const PUT = handler({ access: "signed-in" }, async (request: Request, ctx
saveRecipe(name, body);
return Response.json(recipeView(name));
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/[name]/route.ts b/src/app/api/assist/skills/[name]/route.ts
index a6ebffad..c3ca19bf 100644
--- a/src/app/api/assist/skills/[name]/route.ts
+++ b/src/app/api/assist/skills/[name]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { deleteSkill, getSkill, setSkillEnabled, SkillError, updateSkill } from "@/server/skills";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({
@@ -33,6 +34,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
if (enabled !== undefined) s = setSkillEnabled(s.name, enabled);
return Response.json(s);
} catch (e) {
+ rethrowAccess(e);
return fail(e);
}
});
@@ -42,6 +44,7 @@ export const DELETE = handler({ access: "signed-in" }, async (_req: Request, ctx
deleteSkill((await ctx.params).name);
return new Response(null, { status: 204 });
} catch (e) {
+ rethrowAccess(e);
return fail(e);
}
});
diff --git a/src/app/api/assist/skills/[name]/run/route.ts b/src/app/api/assist/skills/[name]/run/route.ts
index 5ac46bb1..43deba93 100644
--- a/src/app/api/assist/skills/[name]/run/route.ts
+++ b/src/app/api/assist/skills/[name]/run/route.ts
@@ -1,5 +1,6 @@
import { badBody, recipeFailure, RunBody } from "@/server/recipe-routes";
import { runRecipe } from "@/server/recipes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Run a skill's recipe with new inputs, once or once per row, with no LLM. Every row is checked before any starts. */
@@ -10,6 +11,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
const { engine, ...input } = parsed.data;
return Response.json(await runRecipe((await ctx.params).name, input, engine), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/[name]/verify/route.ts b/src/app/api/assist/skills/[name]/verify/route.ts
index f334ba77..484a3076 100644
--- a/src/app/api/assist/skills/[name]/verify/route.ts
+++ b/src/app/api/assist/skills/[name]/verify/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { badBody, recipeFailure } from "@/server/recipe-routes";
import { verifyRecipe } from "@/server/recipes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -18,6 +19,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
const { level, engine, confirm } = parsed.data;
return Response.json(await verifyRecipe((await ctx.params).name, level, engine, { confirm }));
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/draft/check/route.ts b/src/app/api/assist/skills/draft/check/route.ts
index c14d94de..de8cc252 100644
--- a/src/app/api/assist/skills/draft/check/route.ts
+++ b/src/app/api/assist/skills/draft/check/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { badBody, recipeFailure } from "@/server/recipe-routes";
import { checkDraft } from "@/server/recipes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -17,6 +18,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await checkDraft(parsed.data.recipe, parsed.data.level, parsed.data.title || undefined));
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/draft/name/route.ts b/src/app/api/assist/skills/draft/name/route.ts
index c4ecb8d2..b4cd696e 100644
--- a/src/app/api/assist/skills/draft/name/route.ts
+++ b/src/app/api/assist/skills/draft/name/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { badBody, recipeFailure } from "@/server/recipe-routes";
import { draftFromGenerations, nameDraft } from "@/server/recipe-builder";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ generations: z.array(z.string().min(1).max(40)).min(1).max(200) });
@@ -16,6 +17,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
const { draft } = draftFromGenerations(parsed.data.generations);
return Response.json({ naming: draft ? await nameDraft(draft) : null });
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/draft/route.ts b/src/app/api/assist/skills/draft/route.ts
index 60089f34..edadea0d 100644
--- a/src/app/api/assist/skills/draft/route.ts
+++ b/src/app/api/assist/skills/draft/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { badBody, recipeFailure } from "@/server/recipe-routes";
import { draftFromGenerations } from "@/server/recipe-builder";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ generations: z.array(z.string().min(1).max(40)).min(1).max(200) });
@@ -15,6 +16,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(draftFromGenerations(parsed.data.generations));
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/draft/save/route.ts b/src/app/api/assist/skills/draft/save/route.ts
index 122bc154..4c204ae8 100644
--- a/src/app/api/assist/skills/draft/save/route.ts
+++ b/src/app/api/assist/skills/draft/save/route.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { SkillBody } from "@/server/assist/schemas";
import { badBody, recipeFailure } from "@/server/recipe-routes";
import { saveDraft } from "@/server/recipes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = SkillBody.extend({ recipe: z.unknown() });
@@ -13,6 +14,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await saveDraft(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return recipeFailure(e);
}
});
diff --git a/src/app/api/assist/skills/route.ts b/src/app/api/assist/skills/route.ts
index 2f12b3cd..9a7a01b9 100644
--- a/src/app/api/assist/skills/route.ts
+++ b/src/app/api/assist/skills/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { SkillBody } from "@/server/assist/schemas";
import { createSkill, listSkills, SkillError } from "@/server/skills";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Every skill: built-in (from skills/) and the user's own. */
@@ -16,6 +17,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createSkill(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof SkillError ? e.status : 500 });
}
});
diff --git a/src/app/api/assist/skills/upload/route.ts b/src/app/api/assist/skills/upload/route.ts
index 0c5b82d8..a9e8d98f 100644
--- a/src/app/api/assist/skills/upload/route.ts
+++ b/src/app/api/assist/skills/upload/route.ts
@@ -1,4 +1,5 @@
import { importSkill, SkillError } from "@/server/skills";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const MAX_BYTES = 5 * 1024 * 1024;
@@ -14,6 +15,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
const r = importSkill({ name: file.name, data: Buffer.from(await file.arrayBuffer()) }, { replace });
return Response.json(r, { status: r.replaced ? 200 : 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof SkillError ? e.status : 500 });
}
});
diff --git a/src/app/api/audio-tools/export/route.ts b/src/app/api/audio-tools/export/route.ts
index dad4544d..fd31c233 100644
--- a/src/app/api/audio-tools/export/route.ts
+++ b/src/app/api/audio-tools/export/route.ts
@@ -1,4 +1,5 @@
import { MixExportError, PART_MAX, receiveMixPart } from "@/server/mix-export";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -15,6 +16,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
const r = receiveMixPart({ id: q.get("id"), offset, data, last: q.get("last") === "1", name: q.get("name") });
return Response.json(r, { status: "relPath" in r ? 201 : 200 });
} catch (e) {
+ rethrowAccess(e);
if (e instanceof MixExportError) return Response.json({ error: e.message }, { status: e.status });
console.error("[audio-tools/export]", e);
return Response.json({ error: (e as Error).message || "Couldn't save the mix" }, { status: 500 });
diff --git a/src/app/api/audio-tools/route.ts b/src/app/api/audio-tools/route.ts
index 5d0ccca9..ac6441b4 100644
--- a/src/app/api/audio-tools/route.ts
+++ b/src/app/api/audio-tools/route.ts
@@ -1,4 +1,5 @@
import { AudioToolBody, AudioToolError, previewAudioTool, runAudioTool } from "@/server/audio-tools";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** What a field that doesn't check out means, in words the page shows. */
@@ -24,6 +25,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
if (new URL(request.url).searchParams.get("preview") === "1") return Response.json(await previewAudioTool(parsed.data));
return Response.json(await runAudioTool(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
if (e instanceof AudioToolError) return Response.json({ error: e.message }, { status: 422 });
console.error("[audio-tools]", e);
return Response.json({ error: (e as Error).message || "Something went wrong" }, { status: 500 });
diff --git a/src/app/api/auth/password/route.ts b/src/app/api/auth/password/route.ts
index 67d51efe..51860c11 100644
--- a/src/app/api/auth/password/route.ts
+++ b/src/app/api/auth/password/route.ts
@@ -5,6 +5,7 @@ import { PASSWORD_SIGN_IN_MAX } from "@/server/identity/password";
import { listTokens } from "@/server/identity/tokens";
import { signInRequired } from "@/server/mode";
import { currentActor } from "@/server/access/actor";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -38,6 +39,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
await changeOwnerPassword(password, { actorId: actor.userId, via: actor.via });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 400 });
}
const headers = new Headers({ "Cache-Control": "no-store" });
diff --git a/src/app/api/brands/[id]/chat/confirm/route.ts b/src/app/api/brands/[id]/chat/confirm/route.ts
index a29e01ec..27780cd3 100644
--- a/src/app/api/brands/[id]/chat/confirm/route.ts
+++ b/src/app/api/brands/[id]/chat/confirm/route.ts
@@ -2,6 +2,7 @@ import { after } from "next/server";
import { z } from "zod";
import { confirmProposal } from "@/server/ads";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ messageId: z.string().min(1).max(20), engine: z.enum(["cloud", "open"]) });
@@ -16,6 +17,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
after(() => r.run);
return Response.json({ batchId: r.batchId, messages: r.chat }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/brands/[id]/chat/route.ts b/src/app/api/brands/[id]/chat/route.ts
index a14965b2..aa9c6b9e 100644
--- a/src/app/api/brands/[id]/chat/route.ts
+++ b/src/app/api/brands/[id]/chat/route.ts
@@ -3,6 +3,7 @@ import { z } from "zod";
import { chat, clearChat, getChat } from "@/server/ads";
import { getBrand } from "@/server/brand";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ text: z.string().trim().min(1).max(2000) });
@@ -22,6 +23,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json({ messages: await chat(id, body.text) });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/brands/[id]/research/route.ts b/src/app/api/brands/[id]/research/route.ts
index 431e0ce9..3f1d167d 100644
--- a/src/app/api/brands/[id]/research/route.ts
+++ b/src/app/api/brands/[id]/research/route.ts
@@ -1,6 +1,7 @@
import { after } from "next/server";
import { cancelResearch, retryResearch } from "@/server/brand";
import { failure } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Run the website research again (Retry research). */
@@ -11,6 +12,7 @@ export const POST = handler({ access: "signed-in" }, async (_req: Request, ctx:
after(() => run);
return Response.json(brand, { status: 202 });
} catch (e) {
+ rethrowAccess(e);
return (e as Error).message === "Not found" ? Response.json({ error: "Not found" }, { status: 404 }) : failure(e);
}
});
diff --git a/src/app/api/brands/[id]/route.ts b/src/app/api/brands/[id]/route.ts
index ecece0a4..023f3f60 100644
--- a/src/app/api/brands/[id]/route.ts
+++ b/src/app/api/brands/[id]/route.ts
@@ -2,6 +2,7 @@ import { connection } from "next/server";
import { z } from "zod";
import { deleteBrand, getBrand, listProducts, updateBrand } from "@/server/brand";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const list = (max: number, len: number) => z.array(z.string().max(len)).max(max);
@@ -43,6 +44,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const brand = updateBrand(id, body);
return brand ? Response.json(brand) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/brands/route.ts b/src/app/api/brands/route.ts
index dd9bbefd..49a3bdc2 100644
--- a/src/app/api/brands/route.ts
+++ b/src/app/api/brands/route.ts
@@ -2,6 +2,7 @@ import { after, connection } from "next/server";
import { z } from "zod";
import { createBrandFromWebsite, createManualBrand, listBrands } from "@/server/brand";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.union([
@@ -35,6 +36,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
}
return Response.json(await createManualBrand(body), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/canvases/route.ts b/src/app/api/canvases/route.ts
index ea8931f7..06bf4e5f 100644
--- a/src/app/api/canvases/route.ts
+++ b/src/app/api/canvases/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createCanvas, listCanvases, parseGraph, parsePages } from "@/server/canvas";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -26,6 +27,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createCanvas({ ...rest, graph, pages }), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/dub/[id]/route.ts b/src/app/api/dub/[id]/route.ts
index eda8b7b6..0f0ccbb1 100644
--- a/src/app/api/dub/[id]/route.ts
+++ b/src/app/api/dub/[id]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { CueSchema, deleteDubSession, dubView, getDubSession, saveDubDraft } from "@/server/dub";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({ lang: z.string().min(2).max(12), cues: z.array(CueSchema).max(3000) });
@@ -20,6 +21,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
try {
return Response.json(saveDubDraft(id, parsed.data.lang, parsed.data.cues));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: getDubSession(id) ? 422 : 404 });
}
});
diff --git a/src/app/api/dub/[id]/voice/route.ts b/src/app/api/dub/[id]/voice/route.ts
index 3d101d3c..32f64d2d 100644
--- a/src/app/api/dub/[id]/voice/route.ts
+++ b/src/app/api/dub/[id]/voice/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { CueSchema, getDubSession, previewVoiceDub, voiceDub } from "@/server/dub";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -20,6 +21,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
if (new URL(request.url).searchParams.get("preview") === "1") return Response.json(previewVoiceDub(id, parsed.data.lang, opts));
return Response.json(voiceDub(id, parsed.data.lang, opts), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/dub/route.ts b/src/app/api/dub/route.ts
index d9dc6959..ea9d7821 100644
--- a/src/app/api/dub/route.ts
+++ b/src/app/api/dub/route.ts
@@ -2,6 +2,7 @@ import { connection } from "next/server";
import { z } from "zod";
import { ONE_SHOT_MODELS } from "@/lib/av/dub";
import { createDubSession, listDubSessions, previewDub } from "@/server/dub";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -35,6 +36,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
if (new URL(request.url).searchParams.get("preview") === "1") return Response.json(previewDub(parsed.data));
return Response.json(createDubSession(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/elements/[id]/lora/route.ts b/src/app/api/elements/[id]/lora/route.ts
index 38cd3d1f..65928472 100644
--- a/src/app/api/elements/[id]/lora/route.ts
+++ b/src/app/api/elements/[id]/lora/route.ts
@@ -2,6 +2,7 @@ import { z } from "zod";
import { TRAINING_FAMILY_IDS, type TrainingFamily } from "@/lib/catalog/training";
import { attachLora } from "@/server/training";
import { getElement } from "@/server/elements";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -20,6 +21,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
await attachLora(id, parsed.data);
return Response.json(getElement(id));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/elements/[id]/training-kit/route.ts b/src/app/api/elements/[id]/training-kit/route.ts
index dfbdb792..2e6b8a2e 100644
--- a/src/app/api/elements/[id]/training-kit/route.ts
+++ b/src/app/api/elements/[id]/training-kit/route.ts
@@ -1,5 +1,6 @@
import { AITK_FAMILIES, type TrainingFamily } from "@/lib/catalog/training";
import { trainingKit } from "@/server/training";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Dataset + AI-Toolkit config + README, to train the element's LoRA on any GPU (?family= picks the base; Z-Image Turbo by default). */
@@ -13,6 +14,7 @@ export const GET = handler({ access: "signed-in" }, async (req: Request, ctx: Ro
headers: { "Content-Type": "application/zip", "Content-Disposition": `attachment; filename="${kit.name}"` },
});
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/elements/[id]/trainings/route.ts b/src/app/api/elements/[id]/trainings/route.ts
index 31c5f0b3..3973d650 100644
--- a/src/app/api/elements/[id]/trainings/route.ts
+++ b/src/app/api/elements/[id]/trainings/route.ts
@@ -4,6 +4,7 @@ import { targetsFor, TRAINING_TARGETS } from "@/lib/catalog/training";
import { getElement } from "@/server/elements";
import { ensureRunner } from "@/server/generations";
import { describeTargets, listTrainings, startTraining } from "@/server/training";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** The targets this element can train on (each with what stops it, its price and whether it's a demo), and its trainings. */
@@ -29,6 +30,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
ensureRunner();
return Response.json(startTraining(id, parsed.data.target, { consent: parsed.data.consent }), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/enhance/route.ts b/src/app/api/enhance/route.ts
index 57381813..e28a2a8b 100644
--- a/src/app/api/enhance/route.ts
+++ b/src/app/api/enhance/route.ts
@@ -1,5 +1,6 @@
import { enhancePrompt } from "@/server/enhance";
import type { MediaKind, Operation } from "@/lib/catalog/types";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const POST = handler({ access: "signed-in" }, async (request: Request) => {
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json({ prompt: await enhancePrompt(prompt, kind ?? "image", op ?? "t2i") });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 502 });
}
});
diff --git a/src/app/api/folders/[id]/route.ts b/src/app/api/folders/[id]/route.ts
index 08018775..f606f70e 100644
--- a/src/app/api/folders/[id]/route.ts
+++ b/src/app/api/folders/[id]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { deleteFolder, getFolder, LibraryError, updateFolder } from "@/server/library";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({
@@ -27,6 +28,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const f = updateFolder(id, parsed.data);
return f ? Response.json(f) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LibraryError ? e.status : 409 });
}
});
diff --git a/src/app/api/folders/[id]/zip/route.ts b/src/app/api/folders/[id]/zip/route.ts
index 61ffebdf..0d206104 100644
--- a/src/app/api/folders/[id]/zip/route.ts
+++ b/src/app/api/folders/[id]/zip/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { folderZipEntries, LibraryError, ZIP_MAX_BYTES } from "@/server/library";
import { zipSize, zipStream } from "@/server/zip";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -16,6 +17,7 @@ export const GET = handler({ access: "signed-in" }, async (request: Request, ctx
try {
zip = folderZipEntries(id, { steps: sp.get("steps") === "1" });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LibraryError ? e.status : 500 });
}
if (!zip) return Response.json({ error: "That folder doesn't exist anymore" }, { status: 404 });
diff --git a/src/app/api/folders/route.ts b/src/app/api/folders/route.ts
index 038059d7..38138a12 100644
--- a/src/app/api/folders/route.ts
+++ b/src/app/api/folders/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createFolder, LibraryError, listFolders } from "@/server/library";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** `parentId`: a folder's id, or null (or left out) for the library root; "" is refused, not stored as a parent nothing has. */
@@ -19,6 +20,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createFolder(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LibraryError ? e.status : 409 });
}
});
diff --git a/src/app/api/generations/bulk/route.ts b/src/app/api/generations/bulk/route.ts
index 8a27c9c3..0dca611c 100644
--- a/src/app/api/generations/bulk/route.ts
+++ b/src/app/api/generations/bulk/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { deleteForever, moveToFolder, restoreGenerations, setFavorites, setPinned, setReview, trashGenerations } from "@/server/library";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -33,6 +34,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
: setFavorites(ids, action === "favorite");
return Response.json({ count });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/generations/local/route.ts b/src/app/api/generations/local/route.ts
index 80bc018e..a2e1e159 100644
--- a/src/app/api/generations/local/route.ts
+++ b/src/app/api/generations/local/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { recordLocalGeneration } from "@/server/generations";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -19,6 +20,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(recordLocalGeneration(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/generations/route.ts b/src/app/api/generations/route.ts
index ccc2f417..bb24fd77 100644
--- a/src/app/api/generations/route.ts
+++ b/src/app/api/generations/route.ts
@@ -3,6 +3,7 @@ import { OPERATION_LABELS } from "@/lib/catalog/types";
import { createGenerations, listGenerations, outputKind, previewGeneration } from "@/server/generations";
import { ensureTrashPurge, listOptionsFrom } from "@/server/library";
import { GenerationRequestSchema } from "@/server/request-schema";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -36,6 +37,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
const notes = [...new Set(items.flatMap((g) => g.notes ?? []))];
return Response.json({ items, ...(notes.length && { notes }) }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/influencer/route.ts b/src/app/api/influencer/route.ts
index 4e32f99b..5ebc9651 100644
--- a/src/app/api/influencer/route.ts
+++ b/src/app/api/influencer/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { MAX_CLOTHING, MAX_DIRECTION } from "@/lib/influencer";
import { createInfluencer, previewInfluencer, type InfluencerBody } from "@/server/influencer";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const File = z.string().startsWith("/api/files/").max(500);
@@ -39,6 +40,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createInfluencer(body), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/katana/[id]/retry/route.ts b/src/app/api/katana/[id]/retry/route.ts
index 18a44076..bdc740dd 100644
--- a/src/app/api/katana/[id]/retry/route.ts
+++ b/src/app/api/katana/[id]/retry/route.ts
@@ -1,4 +1,5 @@
import { getKatana, rerunKatana, retryKatana } from "@/server/katana";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Retry the failed steps of a run (and the ones after them); `?all=1` starts the same run again from scratch. */
@@ -9,6 +10,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
const all = new URL(request.url).searchParams.get("all") === "1";
return Response.json(all ? rerunKatana(id) : retryKatana(id), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/katana/route.ts b/src/app/api/katana/route.ts
index 26ec5889..2d2f42a8 100644
--- a/src/app/api/katana/route.ts
+++ b/src/app/api/katana/route.ts
@@ -1,5 +1,6 @@
import { connection } from "next/server";
import { createKatana, KatanaBody, listKatana, previewKatana } from "@/server/katana";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Recent Katana runs with the state of every step. */
@@ -21,6 +22,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
if (new URL(request.url).searchParams.get("preview") === "1") return Response.json(previewKatana(parsed.data));
return Response.json(createKatana(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/layers/[id]/jobs/route.ts b/src/app/api/layers/[id]/jobs/route.ts
index 7d900298..2676b637 100644
--- a/src/app/api/layers/[id]/jobs/route.ts
+++ b/src/app/api/layers/[id]/jobs/route.ts
@@ -1,4 +1,5 @@
import { JobInputSchema, LayersError, startJob } from "@/server/layers";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -13,6 +14,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(startJob(id, parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LayersError ? e.status : 422 });
}
});
diff --git a/src/app/api/layers/[id]/route.ts b/src/app/api/layers/[id]/route.ts
index 77c19ed3..c9a51e57 100644
--- a/src/app/api/layers/[id]/route.ts
+++ b/src/app/api/layers/[id]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { deleteProject, LayersError, parseDoc, projectView, updateProject } from "@/server/layers";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({
@@ -31,6 +32,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const p = updateProject(id, { name: parsed.data.name, doc, versionId: parsed.data.versionId, cover: parsed.data.cover });
return p ? Response.json(p) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LayersError ? e.status : 422 });
}
});
diff --git a/src/app/api/layers/[id]/versions/route.ts b/src/app/api/layers/[id]/versions/route.ts
index 8ce535ea..81ff5d15 100644
--- a/src/app/api/layers/[id]/versions/route.ts
+++ b/src/app/api/layers/[id]/versions/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { createVersion, LayersError, parseDoc } from "@/server/layers";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -21,6 +22,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(createVersion(id, { ...parsed.data, doc }), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LayersError ? e.status : 422 });
}
});
diff --git a/src/app/api/layers/route.ts b/src/app/api/layers/route.ts
index 5ea7e9a9..2dd54cf5 100644
--- a/src/app/api/layers/route.ts
+++ b/src/app/api/layers/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createProject, FileUrl, LayersError, listProjects } from "@/server/layers";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -27,6 +28,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createProject(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
const status = e instanceof LayersError ? e.status : 422;
return Response.json({ error: (e as Error).message }, { status });
}
diff --git a/src/app/api/loras/route.ts b/src/app/api/loras/route.ts
index 9a64a345..f09ba400 100644
--- a/src/app/api/loras/route.ts
+++ b/src/app/api/loras/route.ts
@@ -3,6 +3,7 @@ import { LORA_LIBRARY, LORA_LIBRARY_AS_OF } from "@/lib/catalog/lora-library.gen
import { licenceLine } from "@/lib/catalog/loras";
import { downloadToComfy, filesInLoraFolder, listLoraCopies, LoraError } from "@/server/loras";
import { getSetting } from "@/server/settings";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -48,6 +49,7 @@ export const POST = handler({ access: "perm:install.settings" }, async (request:
const copy = await downloadToComfy(body.file);
return Response.json({ file: copy.file, sizeBytes: copy.sizeBytes, sha256: copy.sha256 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: e instanceof LoraError ? 422 : 500 });
}
});
diff --git a/src/app/api/marketing/click-to-ad/estimate/route.ts b/src/app/api/marketing/click-to-ad/estimate/route.ts
index 18300da8..630f3fb5 100644
--- a/src/app/api/marketing/click-to-ad/estimate/route.ts
+++ b/src/app/api/marketing/click-to-ad/estimate/route.ts
@@ -1,4 +1,5 @@
import { ClickToAdBody, estimateClickToAd, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const POST = handler({ access: "signed-in" }, async (request: Request) => {
@@ -7,6 +8,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(estimateClickToAd(body));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ costUsd: null, demo: false, steps: [], notes: [], runs: 0, blocker: (e as Error).message });
}
});
diff --git a/src/app/api/marketing/click-to-ad/route.ts b/src/app/api/marketing/click-to-ad/route.ts
index 97a6c005..7c990637 100644
--- a/src/app/api/marketing/click-to-ad/route.ts
+++ b/src/app/api/marketing/click-to-ad/route.ts
@@ -1,4 +1,5 @@
import { ClickToAdBody, failure, readBody, runClickToAd } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Click to Ad: a 9:16 voiced ad with captions from a product kit and a style. */
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await runClickToAd(body), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/marketing/estimate/route.ts b/src/app/api/marketing/estimate/route.ts
index 4b16a5ac..f0690a44 100644
--- a/src/app/api/marketing/estimate/route.ts
+++ b/src/app/api/marketing/estimate/route.ts
@@ -1,4 +1,5 @@
import { estimateMarketing, MarketingBody, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Price and check a Marketing Studio run before it starts; what's missing comes back as `blocker`. */
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(estimateMarketing(body));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ costUsd: null, demo: false, steps: [], notes: [], runs: 0, blocker: (e as Error).message });
}
});
diff --git a/src/app/api/marketing/runs/[id]/again/route.ts b/src/app/api/marketing/runs/[id]/again/route.ts
index d8916118..83d42d87 100644
--- a/src/app/api/marketing/runs/[id]/again/route.ts
+++ b/src/app/api/marketing/runs/[id]/again/route.ts
@@ -1,4 +1,5 @@
import { failure, rerun } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Run again: the same template, product and settings as a new run. */
@@ -7,6 +8,7 @@ export const POST = handler({ access: "signed-in" }, async (_req: Request, ctx:
try {
return Response.json({ runs: await rerun(id) }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return (e as Error).message === "Not found" ? Response.json({ error: "Not found" }, { status: 404 }) : failure(e);
}
});
diff --git a/src/app/api/marketing/runs/[id]/compose/route.ts b/src/app/api/marketing/runs/[id]/compose/route.ts
index c10ffa2e..51f371d9 100644
--- a/src/app/api/marketing/runs/[id]/compose/route.ts
+++ b/src/app/api/marketing/runs/[id]/compose/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { failure, readBody, saveRunComposite } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -20,6 +21,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(saveRunComposite(id, body), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return (e as Error).message === "Not found" ? Response.json({ error: "Not found" }, { status: 404 }) : failure(e);
}
});
diff --git a/src/app/api/marketing/runs/route.ts b/src/app/api/marketing/runs/route.ts
index ee891e0d..5ce1deae 100644
--- a/src/app/api/marketing/runs/route.ts
+++ b/src/app/api/marketing/runs/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { failure, listRuns, MarketingBody, readBody, runMarketing } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Feature = z.enum(["marketing", "click-to-ad", "ad-multiplier"]);
@@ -21,6 +22,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await runMarketing(body), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/marketing/script/route.ts b/src/app/api/marketing/script/route.ts
index 83fd0bc8..153c7723 100644
--- a/src/app/api/marketing/script/route.ts
+++ b/src/app/api/marketing/script/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { clickToAdScript, failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -17,6 +18,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await clickToAdScript(body));
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/moodboards/[id]/retry/route.ts b/src/app/api/moodboards/[id]/retry/route.ts
index 14e9254a..78778db8 100644
--- a/src/app/api/moodboards/[id]/retry/route.ts
+++ b/src/app/api/moodboards/[id]/retry/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { retryMoodboard } from "@/server/moodboards";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ engine: z.enum(["cloud", "open"]).optional() });
@@ -11,6 +12,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(retryMoodboard(id, parsed.success ? parsed.data.engine : undefined));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: /not found/i.test((e as Error).message) ? 404 : 409 });
}
});
diff --git a/src/app/api/moodboards/[id]/route.ts b/src/app/api/moodboards/[id]/route.ts
index 566517aa..8497a298 100644
--- a/src/app/api/moodboards/[id]/route.ts
+++ b/src/app/api/moodboards/[id]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { deleteMoodboard, getMoodboard, renameMoodboard } from "@/server/moodboards";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({ name: z.string().max(200) });
@@ -19,6 +20,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
try {
return Response.json(renameMoodboard(id, parsed.data.name));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: /not found/i.test((e as Error).message) ? 404 : 400 });
}
});
diff --git a/src/app/api/moodboards/[id]/train/route.ts b/src/app/api/moodboards/[id]/train/route.ts
index 67ca8a92..1ab3fd94 100644
--- a/src/app/api/moodboards/[id]/train/route.ts
+++ b/src/app/api/moodboards/[id]/train/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { moodboardTrainingTargets, trainMoodboard } from "@/server/moodboards";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** The style trainings a board can run (each with its price, what stops it and whether it would be a demo). */
@@ -18,6 +19,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(trainMoodboard(id, parsed.success ? parsed.data?.target : undefined), { status: 202 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: /not found/i.test((e as Error).message) ? 404 : 409 });
}
});
diff --git a/src/app/api/moodboards/route.ts b/src/app/api/moodboards/route.ts
index 132b1739..a5e6497b 100644
--- a/src/app/api/moodboards/route.ts
+++ b/src/app/api/moodboards/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createMoodboard, curatedMoodboards, listMoodboards } from "@/server/moodboards";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -22,6 +23,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createMoodboard(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 400 });
}
});
diff --git a/src/app/api/palettes/[id]/route.ts b/src/app/api/palettes/[id]/route.ts
index b14b2f05..0dbf8965 100644
--- a/src/app/api/palettes/[id]/route.ts
+++ b/src/app/api/palettes/[id]/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { deletePalette, renamePalette } from "@/server/palettes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Patch = z.object({ name: z.string().trim().min(1).max(60) });
@@ -11,6 +12,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
try {
return Response.json(renamePalette(id, parsed.data.name));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: /not found/i.test((e as Error).message) ? 404 : 400 });
}
});
@@ -20,6 +22,7 @@ export const DELETE = handler({ access: "signed-in" }, async (_req: Request, ctx
try {
return deletePalette(id) ? new Response(null, { status: 204 }) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 400 });
}
});
diff --git a/src/app/api/palettes/lock/route.ts b/src/app/api/palettes/lock/route.ts
index d0e97a3b..b39b2a53 100644
--- a/src/app/api/palettes/lock/route.ts
+++ b/src/app/api/palettes/lock/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { createLockedRun, SignatureSchema } from "@/server/palettes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Hex = z.string().regex(/^#?[0-9a-fA-F]{6}$/);
@@ -41,6 +42,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
const { items } = createLockedRun(parsed.data.request as Parameters<typeof createLockedRun>[0], parsed.data.colors, parsed.data.strength);
return Response.json({ items }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/palettes/route.ts b/src/app/api/palettes/route.ts
index 2f1ea391..c2234e6c 100644
--- a/src/app/api/palettes/route.ts
+++ b/src/app/api/palettes/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createPalette, listPalettes, signatureCaps } from "@/server/palettes";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -23,6 +24,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createPalette(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 400 });
}
});
diff --git a/src/app/api/photodump/character/route.ts b/src/app/api/photodump/character/route.ts
index 12c5b0db..f0d66e14 100644
--- a/src/app/api/photodump/character/route.ts
+++ b/src/app/api/photodump/character/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { createSheet, previewSheet, type SheetRun } from "@/server/photodump";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Engine = z.enum(["cloud", "open"]);
@@ -42,6 +43,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createSheet(input), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/photodump/fashion/route.ts b/src/app/api/photodump/fashion/route.ts
index 7476e448..ca56a97b 100644
--- a/src/app/api/photodump/fashion/route.ts
+++ b/src/app/api/photodump/fashion/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { createFashion, previewFashion, type FashionRun } from "@/server/photodump";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const File = z.string().startsWith("/api/files/");
@@ -23,6 +24,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createFashion(input), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/photodump/packs/route.ts b/src/app/api/photodump/packs/route.ts
index d5a5f1f8..4a6a8930 100644
--- a/src/app/api/photodump/packs/route.ts
+++ b/src/app/api/photodump/packs/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createUserPack, listUserPacks } from "@/server/photodump";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ name: z.string().max(200), refs: z.array(z.string().startsWith("/api/files/")).max(40) });
@@ -17,6 +18,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createUserPack(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 400 });
}
});
diff --git a/src/app/api/photodump/retry/route.ts b/src/app/api/photodump/retry/route.ts
index 262789f9..19217314 100644
--- a/src/app/api/photodump/retry/route.ts
+++ b/src/app/api/photodump/retry/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { retryPhoto } from "@/server/photodump";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ id: z.string().min(1).max(40) });
@@ -14,6 +15,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json({ items: retryPhoto(parsed.data.id) }, { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/photodump/route.ts b/src/app/api/photodump/route.ts
index 7b8386d1..417c39b3 100644
--- a/src/app/api/photodump/route.ts
+++ b/src/app/api/photodump/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { z } from "zod";
import { createPhotodump, listUserPacks, previewPhotodump } from "@/server/photodump";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z
@@ -31,6 +32,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createPhotodump(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/products/[id]/route.ts b/src/app/api/products/[id]/route.ts
index 9b97b738..e526169d 100644
--- a/src/app/api/products/[id]/route.ts
+++ b/src/app/api/products/[id]/route.ts
@@ -1,6 +1,7 @@
import { connection } from "next/server";
import { deleteProduct, getProduct, ProductFields, updateProduct } from "@/server/brand";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const GET = handler({ access: "signed-in" }, async (_req: Request, ctx: RouteContext<"/api/products/[id]">) => {
@@ -18,6 +19,7 @@ export const PATCH = handler({ access: "signed-in" }, async (request: Request, c
const p = updateProduct(id, body);
return p ? Response.json(p) : Response.json({ error: "Not found" }, { status: 404 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/products/read/route.ts b/src/app/api/products/read/route.ts
index 22431d20..44e8551e 100644
--- a/src/app/api/products/read/route.ts
+++ b/src/app/api/products/read/route.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { readProductLink } from "@/server/brand";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({
@@ -20,6 +21,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
const r = await readProductLink(body);
return Response.json(r, { status: r.products.length ? 201 : 200 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/products/route.ts b/src/app/api/products/route.ts
index db9011c4..17432d44 100644
--- a/src/app/api/products/route.ts
+++ b/src/app/api/products/route.ts
@@ -2,6 +2,7 @@ import { connection } from "next/server";
import { z } from "zod";
import { createProduct, listProducts, ProductFields } from "@/server/brand";
import { failure, readBody } from "@/server/marketing";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Products: all, a brand's (`?brand=<id>`) or the unassigned ones (`?brand=none`). */
@@ -19,6 +20,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(createProduct({ ...input, price: input.price ?? null }, { force }), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return failure(e);
}
});
diff --git a/src/app/api/providers/test/route.ts b/src/app/api/providers/test/route.ts
index 9c86d925..affc3d2d 100644
--- a/src/app/api/providers/test/route.ts
+++ b/src/app/api/providers/test/route.ts
@@ -3,6 +3,7 @@ import { testCivitaiKey } from "@/server/loras";
import { testAiToolkit } from "@/server/training";
import { PROVIDERS } from "@/server/providers";
import { validateSetting, withCandidates, type SettingName } from "@/server/settings";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/**
@@ -23,6 +24,7 @@ export const POST = handler({ access: "perm:install.settings" }, async (request:
try {
for (const [name, v] of Object.entries(values)) validateSetting(name as SettingName, v);
} catch (e) {
+ rethrowAccess(e);
return Response.json({ ok: false, detail: (e as Error).message }, { status: 400 });
}
const settings: readonly string[] = PROVIDER_SETTINGS[provider];
@@ -35,6 +37,7 @@ export const POST = handler({ access: "perm:install.settings" }, async (request:
const hint = !res.ok && Object.entries(values).map(([n, v]) => settingProblem(n, v ?? "")).find(Boolean);
return Response.json(hint && !res.detail.includes(hint) ? { ...res, detail: `${res.detail} — ${hint}` } : res);
} catch (e) {
+ rethrowAccess(e);
return Response.json({ ok: false, detail: `Couldn't run the test: ${(e as Error).message}` });
}
});
diff --git a/src/app/api/settings/route.ts b/src/app/api/settings/route.ts
index dc85093a..91fd6ac1 100644
--- a/src/app/api/settings/route.ts
+++ b/src/app/api/settings/route.ts
@@ -1,5 +1,6 @@
import { connection } from "next/server";
import { publicSettings, setSetting, SETTINGS, validateSetting, type SettingName } from "@/server/settings";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
export const GET = handler({ access: "signed-in" }, async () => {
@@ -13,6 +14,7 @@ export const PUT = handler({ access: "perm:install.settings" }, async (request:
try {
entries.forEach(([name, value]) => validateSetting(name, value.trim()));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 400 });
}
for (const [name, value] of entries) setSetting(name, value.trim());
diff --git a/src/app/api/storyboards/[id]/film/route.ts b/src/app/api/storyboards/[id]/film/route.ts
index 43296bbd..9a22eb3b 100644
--- a/src/app/api/storyboards/[id]/film/route.ts
+++ b/src/app/api/storyboards/[id]/film/route.ts
@@ -1,5 +1,6 @@
import { z } from "zod";
import { createStoryboardFilm } from "@/server/storyboard";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
const Body = z.object({ modelId: z.string().max(100).optional() });
@@ -11,6 +12,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request, ct
try {
return Response.json(createStoryboardFilm((await ctx.params).id, parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/storyboards/estimate/route.ts b/src/app/api/storyboards/estimate/route.ts
index 6d30b454..6637fa41 100644
--- a/src/app/api/storyboards/estimate/route.ts
+++ b/src/app/api/storyboards/estimate/route.ts
@@ -1,4 +1,5 @@
import { estimateStoryboard, StoryboardBodySchema } from "@/server/storyboard";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** What a board costs before it runs ("Free" on your GPU): every frame, the look's model for frame 1. */
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(estimateStoryboard(parsed.data));
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/app/api/storyboards/route.ts b/src/app/api/storyboards/route.ts
index 89b79659..2cefe884 100644
--- a/src/app/api/storyboards/route.ts
+++ b/src/app/api/storyboards/route.ts
@@ -1,4 +1,5 @@
import { createStoryboard, StoryboardBodySchema } from "@/server/storyboard";
+import { rethrowAccess } from "@/server/access/errors";
import { handler } from "@/server/access/route";
/** Popcorn-style storyboard: consecutive frames, each made from the previous one. */
@@ -8,6 +9,7 @@ export const POST = handler({ access: "signed-in" }, async (request: Request) =>
try {
return Response.json(await createStoryboard(parsed.data), { status: 201 });
} catch (e) {
+ rethrowAccess(e);
return Response.json({ error: (e as Error).message }, { status: 422 });
}
});
diff --git a/src/server/access/errors.ts b/src/server/access/errors.ts
index 587b0736..d1570327 100644
--- a/src/server/access/errors.ts
+++ b/src/server/access/errors.ts
@@ -55,3 +55,11 @@ export class ReauthRequiredError extends AccessError {
super(message, 403, "REAUTH");
}
}
+
+/**
+ * For code that catches errors to answer them itself (a route's `catch (e)`, a studio's `handle()`): pass a refusal on
+ * to handler(), which answers it with its status and audits it. Call it first in the catch block.
+ */
+export function rethrowAccess(e: unknown): void {
+ if (e instanceof AccessError) throw e;
+}
diff --git a/src/server/cinema-schemas.ts b/src/server/cinema-schemas.ts
index fff6d4e1..c0d3eb9b 100644
--- a/src/server/cinema-schemas.ts
+++ b/src/server/cinema-schemas.ts
@@ -2,6 +2,7 @@ import "server-only";
import { z } from "zod";
import { DISTANCE, MAX_KEYS } from "@/lib/camera-path";
import { APERTURE, LENS } from "@/lib/scene3d";
+import { rethrowAccess } from "./access/errors";
import { CinemaError } from "./cinema";
/** Request bodies of /api/cinema/** (zod drops unknown keys). */
@@ -155,6 +156,7 @@ export async function handle(fn: () => Promise<Response> | Response): Promise<Re
try {
return await fn();
} catch (e) {
+ rethrowAccess(e);
if (e instanceof CinemaError) return Response.json({ error: e.message }, { status: e.status });
console.error("[cinema]", e);
return Response.json({ error: (e as Error).message || "Something went wrong" }, { status: 500 });
diff --git a/src/server/content-factory.ts b/src/server/content-factory.ts
index 9508d022..4962d668 100644
--- a/src/server/content-factory.ts
+++ b/src/server/content-factory.ts
@@ -43,6 +43,7 @@ import {
import { genreInfo, pickerData, type ChannelSettings, type EpisodeSettings, type NarratorVoice, type TimedText } from "@/lib/catalog/faceless";
import type { Engine, Generation, GenerationRequest } from "@/lib/catalog/types";
import { probeMedia } from "@/lib/media-probe";
+import { rethrowAccess } from "./access/errors";
import { rowOwner, runAsOwner } from "./access/permissions";
import { authEnabled } from "./auth";
import { db } from "./db";
@@ -878,6 +879,7 @@ export async function handle(fn: () => Promise<Response> | Response): Promise<Re
try {
return await fn();
} catch (e) {
+ rethrowAccess(e);
if (e instanceof NeedsUpload) return Response.json({ error: e.message, needsUpload: true }, { status: e.status });
return facelessHandle(() => {
throw e;
diff --git a/src/server/faceless.ts b/src/server/faceless.ts
index cac29cf3..dce0bb5d 100644
--- a/src/server/faceless.ts
+++ b/src/server/faceless.ts
@@ -79,6 +79,7 @@ import type { Engine, Generation, GenerationRequest } from "@/lib/catalog/types"
import { DocxError, docxText } from "./assist/docx";
import { PdfError, pdfText } from "./assist/pdf";
import { asSystem } from "./access/actor";
+import { rethrowAccess } from "./access/errors";
import { AccountOffError, rowOwner, runAsOwner } from "./access/permissions";
import { db } from "./db";
import { listElements } from "./elements";
@@ -1534,6 +1535,7 @@ export async function handle(fn: () => Promise<Response> | Response): Promise<Re
try {
return await fn();
} catch (e) {
+ rethrowAccess(e);
if (e instanceof FacelessError) return Response.json({ error: e.message }, { status: e.status });
console.error("[faceless]", e);
return Response.json({ error: (e as Error).message || "Something went wrong" }, { status: 500 });
diff --git a/src/server/marketing.ts b/src/server/marketing.ts
index e5d1b056..0331d256 100644
--- a/src/server/marketing.ts
+++ b/src/server/marketing.ts
@@ -49,6 +49,7 @@ import { routesFor } from "@/lib/catalog/plan";
import { probeMedia } from "@/lib/media-probe";
import { themeFrom } from "@/components/ads/layout";
import type { ChainSlot, Engine, GenerationRequest, ModelDef, Operation } from "@/lib/catalog/types";
+import { rethrowAccess } from "./access/errors";
import { getBrand, getProduct } from "./brand";
import { createPipeline, type PipelineStep } from "./chains";
import { db } from "./db";
@@ -1290,6 +1291,7 @@ export async function readBody<S extends z.ZodType>(request: Request, schema: S,
/** A thrown error as a JSON response: 409 for duplicates, 422 for everything the user can fix. */
export function failure(e: unknown): Response {
+ rethrowAccess(e);
const err = e as Error;
const status = err?.name === "DuplicateProductError" || err?.constructor?.name === "DuplicateProductError" ? 409 : 422;
return Response.json({ error: err?.message || "Something went wrong" }, { status });
diff --git a/src/server/recipe-routes.ts b/src/server/recipe-routes.ts
index 2008a30d..1e808b85 100644
--- a/src/server/recipe-routes.ts
+++ b/src/server/recipe-routes.ts
@@ -1,5 +1,6 @@
import "server-only";
import { z } from "zod";
+import { rethrowAccess } from "./access/errors";
import { RecipeError } from "./recipes";
import { SkillError } from "./skills";
@@ -13,6 +14,7 @@ export const RunBody = z.object({
/** A refusal as JSON: the reason, every problem of a recipe, and the status a SkillError carries. */
export function recipeFailure(e: unknown): Response {
+ rethrowAccess(e);
const status = e instanceof SkillError ? e.status : 500;
return Response.json({ error: (e as Error).message, ...(e instanceof RecipeError && { problems: e.problems }) }, { status });
}
diff --git a/src/server/shorts-schemas.ts b/src/server/shorts-schemas.ts
index 7c981d3c..9b0e0061 100644
--- a/src/server/shorts-schemas.ts
+++ b/src/server/shorts-schemas.ts
@@ -2,6 +2,7 @@ import "server-only";
import { z } from "zod";
import { MIXED } from "@/lib/catalog/mixed-media";
import { SHORTS } from "@/lib/catalog/shorts";
+import { rethrowAccess } from "./access/errors";
import { ShortsError } from "./shorts";
/** Request bodies for /api/shorts/** and /api/mixed-media/** (zod), and the shared error answer. */
@@ -64,8 +65,9 @@ export const NonceSchema = z.object({ requestId }).default({});
export const MixedEstimateSchema = MixedBodySchema.extend({ source: source.extend({ url: z.string().max(500).default("/api/files/placeholder.mp4") }) });
-/** A user-facing error answer: ShortsError keeps its status; anything else from the planner is a 422. */
+/** A user-facing error answer: ShortsError keeps its status; anything else from the planner is a 422 (a refusal goes on to handler()). */
export function errorResponse(e: unknown): Response {
+ rethrowAccess(e);
if (e instanceof ShortsError) return Response.json({ error: e.message }, { status: e.status });
return Response.json({ error: (e as Error).message || "Something went wrong" }, { status: 422 });
}
diff --git a/tests/route-sweep.test.ts b/tests/route-sweep.test.ts
index e68eed6f..554fec64 100644
--- a/tests/route-sweep.test.ts
+++ b/tests/route-sweep.test.ts
@@ -71,6 +71,25 @@ describe("every route goes through handler()", () => {
expect(bad).toEqual([]);
});
+ it("passes refusals on: a catch that answers errors itself starts with rethrowAccess, or rethrows what it doesn't answer", () => {
+ const bad: string[] = [];
+ for (const f of FILES) {
+ const lines = fs.readFileSync(f, "utf8").split("\n");
+ lines.forEach((l, i) => {
+ const m = /^(\s*)\}\s*catch\s*\((\w+)\)\s*\{\s*$/.exec(l);
+ if (!m) return;
+ const body: string[] = [];
+ for (let j = i + 1; j < lines.length; j++) {
+ if (lines[j].trim().startsWith("}") && lines[j].search(/\S/) <= m[1].length) break;
+ body.push(lines[j].trim());
+ }
+ if (body[0] === `rethrowAccess(${m[2]});` || body.at(-1) === `throw ${m[2]};`) return;
+ bad.push(`${rel(f)}:${i + 1}`);
+ });
+ }
+ expect(bad).toEqual([]);
+ });
+
it("names the four install-config routes as needing install.settings", () => {
const needs = (file: string, method: string) =>
new RegExp(`export\\s+const\\s+${method}\\s*=\\s*handler\\(\\{\\s*access:\\s*"perm:install\\.settings"`).test(fs.readFileSync(path.join(ROOT, file), "utf8"));