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

[Feature] 에러 토스트 임시 구현 #130

Merged
merged 6 commits into from
Sep 4, 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
8 changes: 8 additions & 0 deletions apps/admin/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import "./global.css";
import "wowds-ui/styles.css";
import "@wow-class/ui/styles.css";
import "react-toastify/dist/ReactToastify.css";

import { JotaiProvider } from "components/JotaiProvider";
import type { Metadata } from "next";
import type { ReactNode } from "react";
import { ToastContainer } from "react-toastify";

export const metadata: Metadata = {
title: {
Expand Down Expand Up @@ -47,6 +49,12 @@ const RootLayout = ({
return (
<html lang="ko">
<body>
<ToastContainer
hideProgressBar
autoClose={4000}
closeButton={false}
limit={1}
/>
<JotaiProvider>
{children}
{modal}
Expand Down
3 changes: 2 additions & 1 deletion apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"react-clock": "^5.0.0",
"react-day-picker": "^9.0.8",
"react-dom": "^18.3.1",
"react-hook-form": "^7.53.0"
"react-hook-form": "^7.53.0",
"react-toastify": "^10.0.5"
},
"devDependencies": {
"@hookform/resolvers": "^3.9.0",
Expand Down
8 changes: 8 additions & 0 deletions apps/client/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import "./global.css";
import "wowds-ui/styles.css";
import "@wow-class/ui/styles.css";
import "react-toastify/dist/ReactToastify.css";

import type { Metadata } from "next";
import { ToastContainer } from "react-toastify";

import { JotaiProvider } from "../components/JotaiProvider";

Expand Down Expand Up @@ -53,6 +55,12 @@ const RootLayout = ({
return (
<html lang="ko">
<body>
<ToastContainer
hideProgressBar
autoClose={4000}
closeButton={false}
limit={1}
/>
<JotaiProvider>{children}</JotaiProvider>
</body>
</html>
Expand Down
1 change: 1 addition & 0 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"next": "^14.2.5",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-toastify": "^10.0.5",
"wowds-icons": "^0.1.3",
"wowds-tokens": "^0.1.1"
},
Expand Down
7 changes: 4 additions & 3 deletions packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
"@wow-class/typescript-config": "workspace:*",
"jest": "^29.7.0",
"jest-fetch-mock": "^3.0.3",
"typescript": "^5.3.3",
"ts-jest": "^29.2.4"
"ts-jest": "^29.2.4",
"typescript": "^5.3.3"
},
"dependencies": {
"next": "^14.2.5"
"next": "^14.2.5",
"react-toastify": "^10.0.5"
}
}
34 changes: 21 additions & 13 deletions packages/utils/src/fetcher/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { toast } from "react-toastify";

type ApiResponse<T = any> = Response & { data?: T; success?: boolean };

type RequestInterceptor = (
Expand All @@ -7,6 +9,8 @@ type ResponseInterceptor<T = any> = (
response: ApiResponse
) => ApiResponse<T> | Promise<ApiResponse<T>>;

const isClient = typeof window !== "undefined";

class Fetcher {
private baseUrl: string;
private defaultHeaders: HeadersInit;
Expand Down Expand Up @@ -68,16 +72,21 @@ class Fetcher {
return response.text();
}

private async handleError(response: Response) {
private async handleError(
response: Response,
data: {
errorCodeName: string;
errorMessage: string;
}
) {
if (!response.ok) {
const text = await response.text();
const error = new Error(
`HTTP Error: ${response.status} ${response.statusText}`
);
(error as any).response = response;
(error as any).responseText = text;

throw error;
const error = new Error();
error.message = data.errorMessage;
error.name = data.errorCodeName;

if (isClient) {
toast.error(error.message);
} else throw error;
hamo-o marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -96,10 +105,11 @@ class Fetcher {

let response: ApiResponse = await fetch(fullUrl, fetchOptions);

await this.handleError(response);
const data = await this.parseJsonResponse(response);
await this.handleError(response, data);

response = await this.interceptResponse(response);
response.data = await this.parseJsonResponse(response);
response.data = data;

return response;
}
Expand Down Expand Up @@ -162,8 +172,6 @@ class Fetcher {
}
}

const isClient = typeof window !== "undefined";

const fetcher = new Fetcher({
baseUrl:
process.env.NEXT_PUBLIC_VERCEL_ENV === "production"
Expand Down
20 changes: 20 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading