-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #148 from woowacourse-teams/feature/CK-187
[feat/CK-187] Error-Boundary를 활용한 에러핸들링(초안)
- Loading branch information
Showing
15 changed files
with
341 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import * as S from './errorComponents.styles'; | ||
import elephantImage from '../../../assets/images/cryingelephant.png'; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
export const NotFound = () => { | ||
const navigate = useNavigate(); | ||
|
||
const moveMainPage = () => { | ||
navigate('/'); | ||
window.location.reload(); | ||
}; | ||
return ( | ||
<S.Container> | ||
<S.ElephantImage src={elephantImage} alt='crying-elephant' /> | ||
<S.NotFoundTitle>404 Not Found</S.NotFoundTitle> | ||
<S.NotFoundText>잘못된 경로 접근했어요</S.NotFoundText> | ||
<S.MovePageButton onClick={moveMainPage}>메인페이지로 돌아가기</S.MovePageButton> | ||
</S.Container> | ||
); | ||
}; | ||
|
||
export const ServerError = () => { | ||
const navigate = useNavigate(); | ||
|
||
const moveMainPage = () => { | ||
navigate('/'); | ||
window.location.reload(); | ||
}; | ||
return ( | ||
<S.Container> | ||
<S.ElephantImage src={elephantImage} alt='crying-elephant' /> | ||
<S.SereverTitle>500 Error</S.SereverTitle> | ||
<S.ServerText>서버에서 오류가 발생했어요</S.ServerText> | ||
<S.MovePageButton onClick={moveMainPage}>메인페이지로 돌아가기</S.MovePageButton> | ||
</S.Container> | ||
); | ||
}; | ||
|
||
export const Runtime = () => { | ||
const navigate = useNavigate(); | ||
|
||
const moveMainPage = () => { | ||
navigate('/'); | ||
window.location.reload(); | ||
}; | ||
return ( | ||
<S.Container> | ||
<S.ElephantImage src={elephantImage} alt='crying-elephant' /> | ||
<S.RuntimeTitle>Error</S.RuntimeTitle> | ||
<S.RuntimeText> | ||
죄송합니다, 페이지를 로드하는 동안 오류가 발생했습니다 | ||
</S.RuntimeText> | ||
<S.MovePageButton onClick={moveMainPage}>메인페이지로 돌아가기</S.MovePageButton> | ||
</S.Container> | ||
); | ||
}; | ||
|
||
export const Critical = () => { | ||
const navigate = useNavigate(); | ||
|
||
const moveMainPage = () => { | ||
navigate('/'); | ||
window.location.reload(); | ||
}; | ||
return ( | ||
<S.Container> | ||
<S.ElephantImage src={elephantImage} alt='crying-elephant' /> | ||
<S.CriticalTitle>Service Not Working</S.CriticalTitle> | ||
<S.CriticalText> | ||
죄송합니다, 알 수 없는 이유로 서비스 사용이 불가능합니다 | ||
</S.CriticalText> | ||
<S.MovePageButton onClick={moveMainPage}>메인페이지로 돌아가기</S.MovePageButton> | ||
</S.Container> | ||
); | ||
}; |
70 changes: 70 additions & 0 deletions
70
client/src/components/_common/error/errorComponents.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Container = styled.section` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
width: 100%; | ||
height: 100vh; | ||
`; | ||
|
||
export const ElephantImage = styled.img` | ||
width: 20%; | ||
`; | ||
|
||
export const NotFoundTitle = styled.h1` | ||
${({ theme }) => theme.fonts.title_large}; | ||
font-size: 4rem; | ||
color: ${({ theme }) => theme.colors.main_dark}; | ||
`; | ||
|
||
export const NotFoundText = styled.h2` | ||
${({ theme }) => theme.fonts.h2}; | ||
color: ${({ theme }) => theme.colors.gray300}; | ||
`; | ||
|
||
export const SereverTitle = styled.h1` | ||
${({ theme }) => theme.fonts.title_large}; | ||
font-size: 4rem; | ||
color: ${({ theme }) => theme.colors.main_dark}; | ||
`; | ||
|
||
export const ServerText = styled.h2` | ||
${({ theme }) => theme.fonts.h2}; | ||
color: ${({ theme }) => theme.colors.gray300}; | ||
`; | ||
|
||
export const RuntimeTitle = styled.h1` | ||
${({ theme }) => theme.fonts.title_large}; | ||
font-size: 4rem; | ||
color: ${({ theme }) => theme.colors.main_dark}; | ||
`; | ||
|
||
export const RuntimeText = styled.h2` | ||
${({ theme }) => theme.fonts.h2}; | ||
color: ${({ theme }) => theme.colors.gray300}; | ||
`; | ||
|
||
export const CriticalTitle = styled.h1` | ||
${({ theme }) => theme.fonts.title_large}; | ||
font-size: 4rem; | ||
color: ${({ theme }) => theme.colors.main_dark}; | ||
`; | ||
|
||
export const CriticalText = styled.h2` | ||
${({ theme }) => theme.fonts.h2}; | ||
color: ${({ theme }) => theme.colors.gray300}; | ||
`; | ||
|
||
export const MovePageButton = styled.button` | ||
${({ theme }) => theme.fonts.button1} | ||
margin: 2rem 0; | ||
padding: 1rem 2rem; | ||
color: ${({ theme }) => theme.colors.white}; | ||
background-color: ${({ theme }) => theme.colors.main_middle}; | ||
border-radius: 30px; | ||
`; |
22 changes: 22 additions & 0 deletions
22
client/src/components/_common/errorBoundary/AsyncBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { PropsWithChildren, Suspense } from 'react'; | ||
import Spinner from '../spinner/Spinner'; | ||
import CriticalErrorBoundary from './CriticalErrorBoundary'; | ||
import NotFoundErrorBoundary from './NotFoundErrorBoundary'; | ||
import RuntimeErrorBoundary from './RuntimeErrorBoundary'; | ||
import ServerErrorBoundary from './ServerErrorBoundary'; | ||
|
||
const AsyncBoundary = ({ children }: PropsWithChildren) => { | ||
return ( | ||
<CriticalErrorBoundary> | ||
<RuntimeErrorBoundary> | ||
<ServerErrorBoundary> | ||
<NotFoundErrorBoundary> | ||
<Suspense fallback={<Spinner />}>{children}</Suspense> | ||
</NotFoundErrorBoundary> | ||
</ServerErrorBoundary> | ||
</RuntimeErrorBoundary> | ||
</CriticalErrorBoundary> | ||
); | ||
}; | ||
|
||
export default AsyncBoundary; |
18 changes: 18 additions & 0 deletions
18
client/src/components/_common/errorBoundary/CriticalErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ErrorInfo } from 'react'; | ||
import { Critical } from '../error/ErrorComponents'; | ||
import ErrorBoundary from './ErrorBoundary'; | ||
|
||
class CriticalErrorBoundary extends ErrorBoundary { | ||
componentDidCatch(_error: any, _errorInfo: ErrorInfo): void {} | ||
|
||
render() { | ||
const { didCatch } = this.state; | ||
const { children } = this.props; | ||
if (didCatch) { | ||
return <Critical />; | ||
} | ||
return children; | ||
} | ||
} | ||
|
||
export default CriticalErrorBoundary; |
38 changes: 13 additions & 25 deletions
38
client/src/components/_common/errorBoundary/ErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
client/src/components/_common/errorBoundary/NotFoundErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ErrorInfo } from 'react'; | ||
import { NotFound } from '../error/ErrorComponents'; | ||
import ErrorBoundary from './ErrorBoundary'; | ||
|
||
class NotFoundErrorBoundary extends ErrorBoundary { | ||
componentDidCatch(error: any, _errorInfo: ErrorInfo): void { | ||
if (error.response.status !== 404) throw error; | ||
} | ||
|
||
render() { | ||
const { didCatch } = this.state; | ||
const { children } = this.props; | ||
if (didCatch) { | ||
return <NotFound />; | ||
} | ||
return children; | ||
} | ||
} | ||
|
||
export default NotFoundErrorBoundary; |
18 changes: 18 additions & 0 deletions
18
client/src/components/_common/errorBoundary/RuntimeErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { ErrorInfo } from 'react'; | ||
import { Runtime } from '../error/ErrorComponents'; | ||
import ErrorBoundary from './ErrorBoundary'; | ||
|
||
class RuntimeErrorBoundary extends ErrorBoundary { | ||
componentDidCatch(_error: any, _errorInfo: ErrorInfo): void {} | ||
|
||
render() { | ||
const { didCatch } = this.state; | ||
const { children } = this.props; | ||
if (didCatch) { | ||
return <Runtime />; | ||
} | ||
return children; | ||
} | ||
} | ||
|
||
export default RuntimeErrorBoundary; |
21 changes: 21 additions & 0 deletions
21
client/src/components/_common/errorBoundary/ServerErrorBoundary.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { SERVER_ERROR_CODE } from '@/constants/_common/regex'; | ||
import { ErrorInfo } from 'react'; | ||
import { ServerError } from '../error/ErrorComponents'; | ||
import ErrorBoundary from './ErrorBoundary'; | ||
|
||
class ServerErrorBoundary extends ErrorBoundary { | ||
componentDidCatch(error: any, _errorInfo: ErrorInfo): void { | ||
if (!SERVER_ERROR_CODE.test(error.response.status)) throw error; | ||
} | ||
|
||
render() { | ||
const { didCatch } = this.state; | ||
const { children } = this.props; | ||
if (didCatch) { | ||
return <ServerError />; | ||
} | ||
return children; | ||
} | ||
} | ||
|
||
export default ServerErrorBoundary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const SERVER_ERROR_CODE = /5\d{2}/; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { ErrorBoundaryContextType } from '@/myTypes/_common/errorBoundary'; | ||
import { createContext } from 'react'; | ||
|
||
export const ErrorBoundaryContext = createContext<ErrorBoundaryContextType | null>(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.