Skip to content

Commit eb8c791

Browse files
committed
chore: handle auth error
1 parent 2191eef commit eb8c791

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

apps/api/src/routers/sessions.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
import { TRPCError } from "@trpc/server";
12
import { router, protectedProcedure, type ProtectedContext } from "../trpc.js";
2-
import { sessionService } from "../services/session.service.js";
3+
import {
4+
sessionService,
5+
AuthorizationError,
6+
} from "../services/session.service.js";
37

48
export const sessionsRouter = router({
59
getAll: protectedProcedure.query(async ({ ctx }) => {
610
const userId = (ctx as ProtectedContext).user.id;
7-
return await sessionService.getSessions(ctx.db.prisma, userId);
11+
12+
try {
13+
return await sessionService.getSessions(ctx.db.prisma, userId);
14+
} catch (error) {
15+
if (error instanceof AuthorizationError) {
16+
throw new TRPCError({
17+
code: "FORBIDDEN",
18+
message: error.message,
19+
});
20+
}
21+
throw error;
22+
}
823
}),
924
});

apps/api/src/services/session.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type SessionWithTopics = Prisma.WeeklySessionGetPayload<{
2121
};
2222
}>;
2323

24-
class AuthorizationError extends Error {
24+
export class AuthorizationError extends Error {
2525
constructor(message: string) {
2626
super(message);
2727
this.name = "AuthorizationError";

0 commit comments

Comments
 (0)