Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
refactor: decouple error handling from API utility functions
Browse files Browse the repository at this point in the history
- API utility functions now throw errors instead of handling toasts
- Error rendering moved to toast.promise in components
- Improved separation of concerns and reusability
- Updated all relevant API functions & components to adhere to the new error handling strategy
  • Loading branch information
kasterra committed May 20, 2024
1 parent 1111173 commit 63e79db
Show file tree
Hide file tree
Showing 43 changed files with 1,375 additions and 1,213 deletions.
25 changes: 16 additions & 9 deletions app/API/admin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { API_SERVER_URL } from "~/util/constant";
import toast from "react-hot-toast";
import { handle401 } from "~/util";
import { EmptyResponse } from "~/types/APIResponse";
import { BadRequestError, ForbiddenError } from "~/util/errors";

export async function setSemester(semester: number, token: string) {
export async function setSemester(
semester: number,
token: string
): Promise<EmptyResponse> {
const response = await fetch(`${API_SERVER_URL}/semester`, {
method: "PUT",
headers: {
Expand All @@ -13,19 +18,21 @@ export async function setSemester(semester: number, token: string) {

switch (response.status) {
case 400:
toast.error("JWT토큰이 없거나 입력값 검증 실패");
throw new BadRequestError("JWT토큰이 없거나 입력값 검증 실패");
break;
case 401:
toast.error("유효하지 않은 JWT 토큰");
handle401();
break;
case 403:
toast.error("관리자만 접근할 수 있는 API 입니다");
throw new ForbiddenError("관리자만 접근할 수 있는 API 입니다");
break;
}
return { ...(await response.json()), status: response.status };
}

export async function getSemester(token: string) {
export async function getSemester(
token: string
): Promise<{ semester: number }> {
const response = await fetch(`${API_SERVER_URL}/semester`, {
method: "GET",
headers: {
Expand All @@ -36,13 +43,13 @@ export async function getSemester(token: string) {

switch (response.status) {
case 400:
toast.error("JWT토큰이 없거나 입력값 검증 실패");
throw new BadRequestError("JWT토큰이 없거나 입력값 검증 실패");
break;
case 401:
toast.error("유효하지 않은 JWT 토큰");
handle401();
break;
case 403:
toast.error("관리자만 접근할 수 있는 API 입니다");
throw new ForbiddenError("관리자만 접근할 수 있는 API 입니다");
break;
}
return { ...(await response.json()), status: response.status };
Expand Down
Loading

0 comments on commit 63e79db

Please sign in to comment.