Skip to content

Commit

Permalink
feat: (#158) ErrorBoundary 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
inyeong-kang committed Aug 8, 2023
1 parent a73eb64 commit 1d02c8a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
10 changes: 7 additions & 3 deletions frontend/src/components/post/PostListPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { AuthContext } from '@hooks/context/auth';
import { useCategoryList } from '@hooks/query/category/useCategoryList';
import { useDrawer } from '@hooks/useDrawer';

import ErrorBoundary from '@pages/ErrorBoundary';

import AddButton from '@components/common/AddButton';
import Dashboard from '@components/common/Dashboard';
import Drawer from '@components/common/Drawer';
Expand Down Expand Up @@ -40,9 +42,11 @@ export default function PostListPage() {
/>
</Drawer>
</S.DrawerWrapper>
<Suspense fallback={<Skeleton />}>
<PostList />
</Suspense>
<ErrorBoundary fallback={<div>에러발생</div>}>
<Suspense fallback={<Skeleton />}>
<PostList />
</Suspense>
</ErrorBoundary>
<S.ButtonContainer>
<UpButton onClick={scrollToTop} />
<S.AddButtonWrapper to={PATH.POST_WRITE}>
Expand Down
39 changes: 39 additions & 0 deletions frontend/src/pages/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Component, ErrorInfo, ReactNode } from 'react';

interface ErrorBoundaryProps {
children: ReactNode;
fallback: ReactNode;
}

interface ErrorBoundaryState {
hasError: boolean;
}

class ErrorBoundary extends Component<ErrorBoundaryProps, ErrorBoundaryState> {
#errorMessage = '';

constructor(props: ErrorBoundaryProps) {
super(props);
this.state = { hasError: false };
}

static getDerivedStateFromError(error: Error) {
return { hasError: true };
}

componentDidCatch(error: Error, errorInfo: ErrorInfo) {
// You can also log the error to an error reporting service
window.console.log(error, errorInfo);
this.#errorMessage = error.message;
}

render() {
if (this.state.hasError) {
return <div>{this.#errorMessage}</div>;
}

return this.props.children;
}
}

export default ErrorBoundary;

0 comments on commit 1d02c8a

Please sign in to comment.