Skip to content
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

[Feat] 어드민 API 예외처리 #147

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions admin/src/apis/authAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@ const headers = {
export const AuthAPI = {
async postAuth(body: PostAuthParams): Promise<PostAuthResponse> {
try {
return new Promise((resolve) =>
resolve({
accessToken: "access token",
})
);
const response = await fetchWithTimeout(`${baseURL}`, {
method: "POST",
headers: headers,
body: JSON.stringify(body),
});

return response.json();
} catch (error) {
console.error("Error:", error);
Expand Down
6 changes: 4 additions & 2 deletions admin/src/hooks/useFetch.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "react";
import { useErrorBoundary } from "react-error-boundary";

export default function useFetch<T, P = void>(fetch: (params: P) => Promise<T>) {
export default function useFetch<T, P = void>(fetch: (params: P) => Promise<T>, showError = true) {
const { showBoundary } = useErrorBoundary();

const [data, setData] = useState<T | null>(null);
Expand All @@ -18,8 +18,10 @@ export default function useFetch<T, P = void>(fetch: (params: P) => Promise<T>)
setIsSuccess(!!data);
} catch (error) {
setIsError(true);
showBoundary(error);
console.error(error);
if (showError) {
showBoundary(error);
}
}
};

Expand Down
12 changes: 10 additions & 2 deletions admin/src/pages/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,27 @@ export default function Login() {
const {
data: token,
isSuccess: isSuccessPostAuth,
isError: isErrorPostAuth,
fetchData: postAuth,
} = useFetch<PostAuthResponse>(() => AuthAPI.postAuth({ adminId: id, password }));
} = useFetch<PostAuthResponse>(() => AuthAPI.postAuth({ adminId: id, password }), false);

const isValidButton = id.length !== 0 && password.length !== 0;

useEffect(() => {
if (isSuccessPostAuth && token) {
setCookie(COOKIE_KEY.ACCESS_TOKEN, token.accessToken);
setCookie(COOKIE_KEY.ACCESS_TOKEN, token.accessToken, { path: "/" });

setErrorMessage("");
navigate("/lottery");
}
}, [isSuccessPostAuth, token]);
useEffect(() => {
if (isErrorPostAuth) {
setErrorMessage(
"아이디 또는 비밀번호가 잘못 되었습니다. 아이디와 비밀번호를 정확히 입력해 주세요."
);
}
}, [isErrorPostAuth]);

const handleChangeId = (e: ChangeEvent<HTMLInputElement>) => {
setId(e.target.value);
Expand Down
14 changes: 7 additions & 7 deletions admin/src/utils/fetchWithTimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ export async function fetchWithTimeout(url: string, options: RequestInit = {}, t
const id = setTimeout(() => controller.abort(), timeout);
options.signal = controller.signal;

try {
const response = await fetch(url, options);
clearTimeout(id);
return response;
} catch (error) {
clearTimeout(id);
throw error;
const response = await fetch(url, options);
clearTimeout(id);

if (!response.ok) {
throw new Error(response.statusText);
}

return response;
}
14 changes: 7 additions & 7 deletions client/src/utils/fetchWithTimeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ export async function fetchWithTimeout(url: string, options: RequestInit = {}, t
const id = setTimeout(() => controller.abort(), timeout);
options.signal = controller.signal;

try {
const response = await fetch(url, options);
clearTimeout(id);
return response;
} catch (error) {
clearTimeout(id);
throw error;
const response = await fetch(url, options);
clearTimeout(id);

if (!response.ok) {
throw new Error(response.statusText);
}

return response;
}
Loading