-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FE] 클라이언트 영역 에러 핸들링 개선 (토스트 UI) #419
Changes from 8 commits
ac9eac5
cec18f9
27b888b
58a4547
4f824b5
8e5d8ac
e0275f9
f381a55
2f7d3bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { FetchOption } from './fetchClient'; | ||
import { fetchClient } from './fetchClient'; | ||
|
||
type FetcherArgs = Omit<FetchOption, 'method'>; | ||
|
||
export const fetcher = { | ||
get: async <T>({ path, isAuthRequire }: FetcherArgs): Promise<T> => { | ||
const response = await fetchClient({ | ||
path, | ||
method: 'GET', | ||
isAuthRequire, | ||
}); | ||
|
||
const data = await response.json(); | ||
|
||
return data.data as T; | ||
}, | ||
post: async ({ path, body, isAuthRequire = false }: FetcherArgs) => { | ||
await fetchClient({ path, method: 'POST', body, isAuthRequire }); | ||
}, | ||
postWithResponse: async <T>({ path, body, isAuthRequire = false }: FetcherArgs): Promise<T> => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] 현재 모든 함수에서
const createFetchClient = (baseUrl: string) => {
return async ({ path, method, body, isAuthRequire = false, headers }: FetchOption) => {
...
};
}; |
||
const response = await fetchClient({ path, method: 'POST', body, isAuthRequire }); | ||
|
||
const data = await response.json(); | ||
|
||
return data.data as T; | ||
}, | ||
delete: async ({ path, isAuthRequire = false }: FetcherArgs) => { | ||
await fetchClient({ path, method: 'DELETE', isAuthRequire }); | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { ResponseError } from '@utils/responseError'; | ||
|
||
import { BASE_URL } from '@constants/api'; | ||
|
||
import { fetchClient } from './_common/fetchClient'; | ||
import { fetcher } from './_common/fetcher'; | ||
|
||
interface UserLoginRequest { | ||
uuid: string; | ||
|
@@ -13,9 +11,8 @@ interface UserLoginRequest { | |
} | ||
|
||
export const postUserLogin = async ({ uuid, request }: UserLoginRequest) => { | ||
const data = await fetchClient<string>({ | ||
const data = await fetcher.postWithResponse<string>({ | ||
path: `/${uuid}/login`, | ||
method: 'POST', | ||
body: request, | ||
isAuthRequire: true, | ||
}); | ||
|
@@ -30,22 +27,5 @@ export const postUserLogin = async ({ uuid, request }: UserLoginRequest) => { | |
* TODO: 응답 데이터가 없을 때도 대응 가능한 fetchClient 함수를 만들어야 함 | ||
*/ | ||
Comment on lines
27
to
28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P3] 이 주석은 이제 지워도 될 것 같네요 :) |
||
export const postUserLogout = async (uuid: string) => { | ||
try { | ||
const response = await fetch(`${BASE_URL}/${uuid}/logout`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
credentials: 'include', | ||
}); | ||
|
||
if (!response.ok) { | ||
const data = await response.json(); | ||
|
||
throw new ResponseError(data); | ||
} | ||
} catch (error) { | ||
console.error('로그아웃 중 문제가 발생했습니다:', error); | ||
throw error; | ||
} | ||
await fetcher.post({ path: `${BASE_URL}/${uuid}/logout`, isAuthRequire: true }); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
import { useEffect, useRef } from 'react'; | ||
|
||
import useErrorState from '@hooks/useErrorState/useErrorState'; | ||
import useToast from '@hooks/useToast/useToast'; | ||
|
||
export default function ErrorToastNotifier({ children }: PropsWithChildren) { | ||
const error = useErrorState(); | ||
const { addToast } = useToast(); | ||
|
||
const addToastCallbackRef = useRef< | ||
(({ type, message, duration }: Parameters<typeof addToast>[0]) => void) | null | ||
>(null); | ||
addToastCallbackRef.current = addToast; | ||
|
||
useEffect(() => { | ||
if (!error || !addToastCallbackRef.current) return; | ||
|
||
addToastCallbackRef.current({ type: 'warning', message: error.message, duration: 3000 }); | ||
}, [error]); | ||
|
||
return children; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { type PropsWithChildren } from 'react'; | ||
|
||
import useErrorDispatch from '@hooks/useErrorDispatch/useErrorDispatch'; | ||
|
||
import { ResponseError } from '@utils/responseError'; | ||
|
||
export default function QueryClientManager({ children }: PropsWithChildren) { | ||
const setError = useErrorDispatch(); | ||
|
||
const queryClient = new QueryClient({ | ||
defaultOptions: { | ||
queries: { | ||
throwOnError: true, | ||
}, | ||
mutations: { | ||
onError: (error: unknown) => { | ||
if (error instanceof ResponseError) { | ||
setError(error); | ||
} | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P3]
이 부분이 중복되는데, 아래처럼 함수로 분리해볼 수도 있을 것 같아요~!