feat: implement error boundaries for improved error handling across routes

This commit is contained in:
Ardeman 2025-03-11 23:04:29 +08:00
parent 2b253633a5
commit d1b828bba1
13 changed files with 371 additions and 4 deletions

View File

@ -16,7 +16,7 @@ export const createSubscribePlanSchema = z.object({
code: z.string(), code: z.string(),
length: z.preprocess(Number, z.number().optional()), length: z.preprocess(Number, z.number().optional()),
price: z.preprocess(Number, z.number().optional()), price: z.preprocess(Number, z.number().optional()),
status: z.boolean().optional(), status: z.number().optional(),
}) })
export type TSubscribePlanSchema = z.infer<typeof createSubscribePlanSchema> export type TSubscribePlanSchema = z.infer<typeof createSubscribePlanSchema>
type TProperties = { type TProperties = {

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getCategories } from '~/apis/common/get-categories' import { getCategories } from '~/apis/common/get-categories'
import { FormCategoryPage } from '~/pages/form-category' import { FormCategoryPage } from '~/pages/form-category'
@ -11,6 +13,35 @@ export const loader = async ({ params }: Route.LoaderArgs) => {
return { categoryData } return { categoryData }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardCategoriesUpdateLayout = ({ const DashboardCategoriesUpdateLayout = ({
loaderData, loaderData,
}: Route.ComponentProps) => { }: Route.ComponentProps) => {

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getNews } from '~/apis/common/get-news' import { getNews } from '~/apis/common/get-news'
import { ContentsPage } from '~/pages/dashboard-contents' import { ContentsPage } from '~/pages/dashboard-contents'
@ -8,5 +10,34 @@ export const loader = async ({}: Route.LoaderArgs) => {
return { newsData } return { newsData }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardContentsIndexLayout = () => <ContentsPage /> const DashboardContentsIndexLayout = () => <ContentsPage />
export default DashboardContentsIndexLayout export default DashboardContentsIndexLayout

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getNewsBySlug } from '~/apis/common/get-news-by-slug' import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
import { handleCookie } from '~/libs/cookies' import { handleCookie } from '~/libs/cookies'
import { FormContentsPage } from '~/pages/form-contents' import { FormContentsPage } from '~/pages/form-contents'
@ -13,6 +15,35 @@ export const loader = async ({ request, params }: Route.LoaderArgs) => {
return { newsData } return { newsData }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardContentUpdateLayout = ({ loaderData }: Route.ComponentProps) => { const DashboardContentUpdateLayout = ({ loaderData }: Route.ComponentProps) => {
const { newsData } = loaderData || {} const { newsData } = loaderData || {}
return <FormContentsPage newsData={newsData} /> return <FormContentsPage newsData={newsData} />

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getSubscriptions } from '~/apis/common/get-subscriptions' import { getSubscriptions } from '~/apis/common/get-subscriptions'
import { SubscribePlanPage } from '~/pages/dashboard-plan-subscribe' import { SubscribePlanPage } from '~/pages/dashboard-plan-subscribe'
@ -7,5 +9,35 @@ export const loader = async ({}: Route.LoaderArgs) => {
const { data: subscriptionsData } = await getSubscriptions() const { data: subscriptionsData } = await getSubscriptions()
return { subscriptionsData } return { subscriptionsData }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardSubscriptionsSettingsLayout = () => <SubscribePlanPage /> const DashboardSubscriptionsSettingsLayout = () => <SubscribePlanPage />
export default DashboardSubscriptionsSettingsLayout export default DashboardSubscriptionsSettingsLayout

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getSubscriptions } from '~/apis/common/get-subscriptions' import { getSubscriptions } from '~/apis/common/get-subscriptions'
import { FormSubscribePlanPage } from '~/pages/form-subscriptions-plan' import { FormSubscribePlanPage } from '~/pages/form-subscriptions-plan'
@ -11,6 +13,35 @@ export const loader = async ({ params }: Route.LoaderArgs) => {
return { subscribePlanData } return { subscribePlanData }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardSubscribePlanUpdateLayout = ({ const DashboardSubscribePlanUpdateLayout = ({
loaderData, loaderData,
}: Route.ComponentProps) => { }: Route.ComponentProps) => {

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getTags } from '~/apis/common/get-tags' import { getTags } from '~/apis/common/get-tags'
import { FormTagPage } from '~/pages/form-tag' import { FormTagPage } from '~/pages/form-tag'
@ -9,6 +11,35 @@ export const loader = async ({ params }: Route.LoaderArgs) => {
return { tagData } return { tagData }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardTagUpdateLayout = ({ loaderData }: Route.ComponentProps) => { const DashboardTagUpdateLayout = ({ loaderData }: Route.ComponentProps) => {
const { tagData } = loaderData || {} const { tagData } = loaderData || {}
return <FormTagPage tagData={tagData} /> return <FormTagPage tagData={tagData} />

View File

@ -1,4 +1,4 @@
import { Outlet } from 'react-router' import { isRouteErrorResponse, Outlet } from 'react-router'
import { getCategories } from '~/apis/common/get-categories' import { getCategories } from '~/apis/common/get-categories'
import { getTags } from '~/apis/common/get-tags' import { getTags } from '~/apis/common/get-tags'
@ -17,6 +17,35 @@ export const loader = async ({}: Route.LoaderArgs) => {
} }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const DashboardLayout = () => { const DashboardLayout = () => {
return ( return (
<AdminProvider> <AdminProvider>

View File

@ -1,4 +1,4 @@
import { Outlet, redirect } from 'react-router' import { isRouteErrorResponse, Outlet, redirect } from 'react-router'
import { XiorError } from 'xior' import { XiorError } from 'xior'
import { getStaff } from '~/apis/admin/get-staff' import { getStaff } from '~/apis/admin/get-staff'
@ -41,6 +41,35 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
} }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const AdminLayout = () => { const AdminLayout = () => {
return ( return (
<AdminDefaultLayout> <AdminDefaultLayout>

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getCategories } from '~/apis/common/get-categories' import { getCategories } from '~/apis/common/get-categories'
import { getNews } from '~/apis/common/get-news' import { getNews } from '~/apis/common/get-news'
import { NewsPage } from '~/pages/news' import { NewsPage } from '~/pages/news'
@ -35,6 +37,35 @@ export const loader = async ({}: Route.LoaderArgs) => {
} }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const NewsIndexLayout = () => <NewsPage /> const NewsIndexLayout = () => <NewsPage />
export default NewsIndexLayout export default NewsIndexLayout

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getCategories } from '~/apis/common/get-categories' import { getCategories } from '~/apis/common/get-categories'
import { getNews } from '~/apis/common/get-news' import { getNews } from '~/apis/common/get-news'
import { APP } from '~/configs/meta' import { APP } from '~/configs/meta'
@ -26,6 +28,35 @@ export const meta = ({ data }: Route.MetaArgs) => {
] ]
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const NewsCategoriesLayout = () => <NewsCategoriesPage /> const NewsCategoriesLayout = () => <NewsCategoriesPage />
export default NewsCategoriesLayout export default NewsCategoriesLayout

View File

@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getCategories } from '~/apis/common/get-categories' import { getCategories } from '~/apis/common/get-categories'
import { getNews } from '~/apis/common/get-news' import { getNews } from '~/apis/common/get-news'
import { getNewsBySlug } from '~/apis/common/get-news-by-slug' import { getNewsBySlug } from '~/apis/common/get-news-by-slug'
@ -39,6 +41,35 @@ export const meta = ({ data }: Route.MetaArgs) => {
] ]
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const NewsDetailLayout = () => <NewsDetailPage /> const NewsDetailLayout = () => <NewsDetailPage />
export default NewsDetailLayout export default NewsDetailLayout

View File

@ -1,4 +1,4 @@
import { Outlet } from 'react-router' import { isRouteErrorResponse, Outlet } from 'react-router'
import { XiorError } from 'xior' import { XiorError } from 'xior'
import { getCategories } from '~/apis/common/get-categories' import { getCategories } from '~/apis/common/get-categories'
@ -36,6 +36,35 @@ export const loader = async ({ request }: Route.LoaderArgs) => {
} }
} }
export const ErrorBoundary = ({ error }: Route.ErrorBoundaryProps) => {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined
if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404
? 'The requested page could not be found.'
: error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}
return (
<div className="container mx-auto p-4">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 whitespace-pre-wrap">
<code>{stack}</code>
</pre>
)}
</div>
)
}
const NewsLayout = () => { const NewsLayout = () => {
return ( return (
<NewsProvider> <NewsProvider>