A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search
fix: Better handling for body JSON parsing errors
Mohamed Bassemf5fd3c48b7f3
diff --git a/apps/web/app/api/v1/utils/handler.ts b/apps/web/app/api/v1/utils/handler.tsindex d5f470df..84847d71 100644--- a/apps/web/app/api/v1/utils/handler.ts+++ b/apps/web/app/api/v1/utils/handler.ts@@ -98,7 +98,24 @@ export async function buildHandler<let body: SchemaType<BodyT> | undefined = undefined;if (bodySchema) {- body = bodySchema.parse(await req.json()) as SchemaType<BodyT>;+ if (req.headers.get("Content-Type") !== "application/json") {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: "Content-Type must be application/json",+ });+ }++ let bodyJson = undefined;+ try {+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment+ bodyJson = await req.json();+ } catch (e) {+ throw new TRPCError({+ code: "BAD_REQUEST",+ message: `Invalid JSON: ${(e as Error).message}`,+ });+ }+ body = bodySchema.parse(bodyJson) as SchemaType<BodyT>;}const { status, resp } = await handler({@@ -138,6 +155,10 @@ export async function buildHandler<},});} else {+ const error = e as Error;+ console.error(+ `Unexpected error in: ${req.method} ${req.nextUrl.pathname}:\n${error.stack}`,+ );return new Response(JSON.stringify({ code: "UnknownError" }), {status: 500,headers: {