Skip to content

Commit

Permalink
[C-3632] Return null instead of NOT_FOUND in trpc-server (#7184)
Browse files Browse the repository at this point in the history
  • Loading branch information
amendelsohn authored and dylanjeffers committed Jan 16, 2024
1 parent 624bb06 commit 5da8d3a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
3 changes: 1 addition & 2 deletions packages/trpc-server/src/routers/playlist-router.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { z } from 'zod'
import { publicProcedure, router } from '../trpc'
import { TRPCError } from '@trpc/server'

export const playlistRouter = router({
get: publicProcedure.input(z.string()).query(async ({ ctx, input }) => {
const row = await ctx.loaders.playlistLoader.load(parseInt(input))
if (!row) {
throw new TRPCError({ code: 'NOT_FOUND' })
return null;
}
return row
}),
Expand Down
5 changes: 2 additions & 3 deletions packages/trpc-server/src/routers/track-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { z } from 'zod'
import { publicProcedure, router } from '../trpc'
import { TRPCError } from '@trpc/server'
import { sql } from '../db'
import { UserRow } from '../db-tables'
import { esc } from './search-router'
Expand All @@ -15,7 +14,7 @@ export const trackRouter = router({
get: publicProcedure.input(z.string()).query(async ({ ctx, input }) => {
const row = await ctx.loaders.trackLoader.load(parseInt(input))
if (!row) {
throw new TRPCError({ code: 'NOT_FOUND' })
return null;
}
return row
}),
Expand Down Expand Up @@ -88,7 +87,7 @@ export const trackRouter = router({

const hits = found.hits.hits.map((h) => h._source)
if (hits.length === 0) {
throw new TRPCError({ code: 'NOT_FOUND' })
return null;
}
return hits[0]
}),
Expand Down
8 changes: 2 additions & 6 deletions packages/trpc-server/src/routers/user-router.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import { TRPCError } from '@trpc/server'
import { z } from 'zod'
import {
selectPlaylistsCamel,
selectTracksCamel,
selectUsersCamel,
sql
} from '../db'
import { AggregateUserRow, AggregateUserTipRow } from '../db-tables'
import { AggregateUserTipRow } from '../db-tables'
import { publicProcedure, router } from '../trpc'

export const userRouter = router({
get: publicProcedure.input(z.string()).query(async ({ ctx, input }) => {
const user = await ctx.loaders.userLoader.load(parseInt(input))
if (!user) {
throw new TRPCError({
code: 'NOT_FOUND',
message: `user ${input} not found`
})
return null;
}
return user
}),
Expand Down
4 changes: 4 additions & 0 deletions packages/trpc-server/test/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ test('get user', async () => {
const caller = await testRouter(101)

const steve = await caller.users.get('101')
if (!steve) throw new Error('steve not found')
expect(steve.handle).toEqual('steve')
expect(steve.trackCount).toEqual(2)
expect(steve.playlistCount).toEqual(1)
expect(steve.followerCount).toEqual(0)
expect(steve.followingCount).toEqual(1)

const dave = await caller.users.get('102')
if (!dave) throw new Error('dave not found')
expect(dave.handle).toEqual('dave')
expect(dave.followerCount).toEqual(1)
expect(dave.followingCount).toEqual(0)
Expand All @@ -45,13 +47,15 @@ test('get user', async () => {
test('get track', async () => {
const caller = await testRouter(101)
const t = await caller.tracks.get('201')
if (!t) throw new Error('track not found')
expect(t.title).toEqual('Who let the dogs out')
})

test("dave tries to get steve's private track", async () => {
// TODO: this should probably 404...
const daveRouter = await testRouter(201)
const t = await daveRouter.tracks.get('202')
if (!t) throw new Error('track not found')
expect(t.title).toEqual("Steve's unlisted dogs track")
expect(t.isUnlisted).toEqual(true)
})

0 comments on commit 5da8d3a

Please sign in to comment.