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
+31
View File
@@ -1,3 +1,5 @@
import { isRouteErrorResponse } from 'react-router'
import { getCategories } from '~/apis/common/get-categories'
import { getNews } from '~/apis/common/get-news'
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 />
export default NewsDetailLayout