From 8fd839a7ead19f562190bf256b010789cdcba0d9 Mon Sep 17 00:00:00 2001 From: pakxe Date: Sat, 17 Aug 2024 00:50:13 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20ErrorProvider=EC=97=90=EC=84=9C=20u?= =?UTF-8?q?seError=EB=A5=BC=20=ED=8C=8C=EC=9D=BC=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/ErrorProvider.tsx | 83 -------------------------- client/src/hooks/useError/useError.tsx | 12 ++++ 2 files changed, 12 insertions(+), 83 deletions(-) delete mode 100644 client/src/ErrorProvider.tsx create mode 100644 client/src/hooks/useError/useError.tsx diff --git a/client/src/ErrorProvider.tsx b/client/src/ErrorProvider.tsx deleted file mode 100644 index c657d9337..000000000 --- a/client/src/ErrorProvider.tsx +++ /dev/null @@ -1,83 +0,0 @@ -import {createContext, useState, useContext, useEffect, ReactNode} from 'react'; - -import {SERVER_ERROR_MESSAGES} from '@constants/errorMessage'; - -// 에러 컨텍스트 생성 -interface ErrorContextType { - hasError: boolean; - errorMessage: string; - setError: (error: ServerError) => void; - clearError: (ms?: number) => void; - error: ServerError | null; -} - -const ErrorContext = createContext(undefined); - -// 에러 컨텍스트를 제공하는 프로바이더 컴포넌트 -interface ErrorProviderProps { - children: ReactNode; - callback?: (message: string) => void; -} - -export type ServerError = { - errorCode: string; - message: string; -}; - -export const ErrorProvider = ({children, callback}: ErrorProviderProps) => { - const [hasError, setHasError] = useState(false); - const [errorMessage, setErrorMessage] = useState(''); - const [error, setErrorState] = useState(null); - - useEffect(() => { - if (error) { - if (isUnhandledError(error.errorCode)) { - // 에러바운더리로 보내기 - - throw error; - } - - setHasError(true); - const message = SERVER_ERROR_MESSAGES[error.errorCode]; - setErrorMessage(message); - // callback(message); - } - }, [error, callback]); - - const setError = (error: ServerError) => { - setHasError(true); - setErrorMessage(''); - setErrorState(error); - }; - - const clearError = (ms: number = 0) => { - if (error === null) return; - - setTimeout(() => { - setHasError(false); - setErrorMessage(''); - setErrorState(null); - }, ms); - }; - - return ( - - {children} - - ); -}; - -// 에러 컨텍스트를 사용하는 커스텀 훅 -export const useError = (): ErrorContextType => { - const context = useContext(ErrorContext); - if (!context) { - throw new Error('useError must be used within an ErrorProvider'); - } - return context; -}; - -const isUnhandledError = (errorCode: string) => { - if (errorCode === 'INTERNAL_SERVER_ERROR') return true; - - return SERVER_ERROR_MESSAGES[errorCode] === undefined; -}; diff --git a/client/src/hooks/useError/useError.tsx b/client/src/hooks/useError/useError.tsx new file mode 100644 index 000000000..ee7176233 --- /dev/null +++ b/client/src/hooks/useError/useError.tsx @@ -0,0 +1,12 @@ +import {useContext} from 'react'; + +import {ErrorContext, ErrorContextType} from './ErrorProvider'; + +// 에러 컨텍스트를 사용하는 커스텀 훅 +export const useError = (): ErrorContextType => { + const context = useContext(ErrorContext); + if (!context) { + throw new Error('useError must be used within an ErrorProvider'); + } + return context; +};