From 8895fc31b47c6d96d5a2ac860a94936086eb6a46 Mon Sep 17 00:00:00 2001 From: Brandon Egger Date: Tue, 29 Aug 2023 10:30:18 -0500 Subject: [PATCH] update to raise NOT_FOUND error when no resource found --- src/components/LoadingWrapper.tsx | 2 ++ src/pages/resources/[id]/edit.tsx | 10 ++++-- src/server/api/routers/auditoryResources.ts | 36 +++++++++++++-------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/components/LoadingWrapper.tsx b/src/components/LoadingWrapper.tsx index 3aefbcb..36f909c 100644 --- a/src/components/LoadingWrapper.tsx +++ b/src/components/LoadingWrapper.tsx @@ -18,6 +18,8 @@ export function QueryWaitWrapper({ return ; } + console.log(query.data); + if (!query.data || query.isError) { return (
diff --git a/src/pages/resources/[id]/edit.tsx b/src/pages/resources/[id]/edit.tsx index c6caae9..82b1634 100644 --- a/src/pages/resources/[id]/edit.tsx +++ b/src/pages/resources/[id]/edit.tsx @@ -12,7 +12,7 @@ import { api } from "~/utils/api"; import { useRouter } from "next/router"; import { HeaderFooterLayout } from "~/layouts/HeaderFooterLayout"; import { QueryWaitWrapper } from "~/components/LoadingWrapper"; -import { AuditoryResource } from "@prisma/client"; +import { type AuditoryResource } from "@prisma/client"; const EditResourcePage = () => { const router = useRouter(); @@ -20,7 +20,13 @@ const EditResourcePage = () => { const resourceQuery = api.auditoryResource.byId.useQuery( { id }, - { enabled: router.isReady } + { + enabled: router.isReady, + onError(err) { + console.log(err); + throw err; + }, + } ); const ConditionalView = (data: AuditoryResource) => { diff --git a/src/server/api/routers/auditoryResources.ts b/src/server/api/routers/auditoryResources.ts index d18e609..00d833b 100644 --- a/src/server/api/routers/auditoryResources.ts +++ b/src/server/api/routers/auditoryResources.ts @@ -1,10 +1,5 @@ -import { - SkillLevel, - Skill, - Platform, - type AuditoryResource, - PaymentType, -} from "@prisma/client"; +import { SkillLevel, Skill, Platform, PaymentType } from "@prisma/client"; +import { TRPCError } from "@trpc/server"; import { z } from "zod"; import { @@ -25,13 +20,28 @@ export const auditoryResourceRouter = createTRPCRouter({ byId: publicProcedure .input(z.object({ id: z.string() })) .query(async ({ input, ctx }) => { - const resource = await ctx.prisma.auditoryResource.findUnique({ - where: { - id: input.id, - }, - }); + try { + const resource = await ctx.prisma.auditoryResource.findUnique({ + where: { + id: input.id, + }, + }); - return { ...resource } as AuditoryResource; + if (!resource) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "The resource you are looking for was not found.", + }); + } + + return resource; + } catch (e) { + throw new TRPCError({ + code: "NOT_FOUND", + message: "The resource you are looking for was not found.", + cause: e, + }); + } }), getAll: publicProcedure.query(({ ctx }) => {