Skip to content

Commit

Permalink
refactor: update error handling to use data responses for not found a…
Browse files Browse the repository at this point in the history
…nd unauthorized cases
  • Loading branch information
coji committed Dec 2, 2024
1 parent c3def60 commit 5045909
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/routes/$handle+/_index/route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MoreVerticalIcon, PlusIcon } from 'lucide-react'
import React from 'react'
import {
data,
Form,
Link,
redirect,
Expand Down Expand Up @@ -34,7 +35,7 @@ export const clientLoader = async ({
const { handle } = zx.parseParams(params, { handle: z.string() })

const isExist = await isAccountExistsByHandle(handle)
if (!isExist) throw new Error('Not found')
if (!isExist) throw data(null, { status: 404 })

const user = await isAuthenticated(request)
const posts = await listUserPosts(handle)
Expand Down
4 changes: 2 additions & 2 deletions app/routes/$handle+/posts.$id._index/route.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArrowLeftIcon, PencilIcon } from 'lucide-react'
import ReactMarkdown from 'react-markdown'
import { Link } from 'react-router'
import { data, Link } from 'react-router'
import { $path } from 'remix-routes'
import { z } from 'zod'
import { zx } from 'zodix'
Expand All @@ -21,7 +21,7 @@ export const clientLoader = async ({
})

const post = await getUserPostById(handle, id)
if (!post) throw new Error('Not found')
if (!post) throw data(null, { status: 404 })

const user = await isAuthenticated(request)

Expand Down
4 changes: 2 additions & 2 deletions app/routes/$handle+/posts.$id.delete/route.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { redirect, useFetcher } from 'react-router'
import { data, redirect, useFetcher } from 'react-router'
import { $path } from 'remix-routes'
import { z } from 'zod'
import { zx } from 'zodix'
Expand Down Expand Up @@ -29,7 +29,7 @@ export const clientAction = async ({

const user = await requireUser(request, { failureRedirect: $path('/') })
if (user.handle !== handle) {
throw new Response('Unauthorized', { status: 401 })
throw data(null, { status: 401 })
}
await deleteUserPost(handle, id)
return redirect($path('/:handle', { handle }))
Expand Down
8 changes: 4 additions & 4 deletions app/routes/$handle+/posts.$id.edit/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@conform-to/react'
import { parseWithZod } from '@conform-to/zod'
import { ArrowLeftIcon } from 'lucide-react'
import { Form, Link, redirect } from 'react-router'
import { data, Form, Link, redirect } from 'react-router'
import { $path } from 'remix-routes'
import { z } from 'zod'
import { AppHeadingSection } from '~/components/AppHeadingSection'
Expand Down Expand Up @@ -36,10 +36,10 @@ export const clientLoader = async ({
}: Route.ClientLoaderArgs) => {
// 本人の投稿以外は編集できない / 存在確認
const user = await requireUser(request, { failureRedirect: $path('/') })
if (handle !== user.handle) throw new Response('Forbidden', { status: 403 })
if (handle !== user.handle) throw data(null, { status: 403 })

const post = await getUserPostById(handle, id)
if (!post) throw new Response('Not found', { status: 404 })
if (!post) throw data(null, { status: 404 })
return { handle, id, post, user }
}

Expand All @@ -49,7 +49,7 @@ export const clientAction = async ({
}: Route.ClientActionArgs) => {
// 本人の投稿以外は編集できない / 存在確認
const user = await requireUser(request, { failureRedirect: $path('/') })
if (handle !== user.handle) throw { message: 'Forbidden', status: 403 }
if (handle !== user.handle) throw data(null, { status: 403 })

const submission = parseWithZod(await request.formData(), { schema })
if (submission.status !== 'success') {
Expand Down

0 comments on commit 5045909

Please sign in to comment.