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

connect upload component's error to message #163

Merged
merged 9 commits into from
Dec 29, 2024
Merged
47 changes: 22 additions & 25 deletions app/business/services/lecture/taken-lecture.command.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
'use server';
import { FormState } from '@/app/ui/view/molecule/form/form-root';
import { API_PATH } from '../../api-path';
import { BadRequestError } from '@/app/utils/http/http-error';
import { cookies } from 'next/headers';
import { redirect } from 'next/navigation';
import { BadRequestError, HttpError } from '@/app/utils/http/http-error';
import { instance } from '@/app/utils/api/instance';
import { ERROR_CODE } from '@/app/utils/api/constant';

export const registerUserGrade = async (prevState: FormState, formData: FormData) => {
const parsingText = await parsePDFtoText(formData);

const res = await fetch(API_PATH.registerUserGrade, {
method: 'POST',
body: JSON.stringify({ parsingText }),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${cookies().get('accessToken')?.value}`,
},
});

if (!res.ok) {
try {
const parsingText = await parsePDFtoText(formData);
await instance.post(API_PATH.registerUserGrade, { parsingText });
return {
isSuccess: true,
isFailure: false,
validationError: {},
message: '',
};
} catch (error) {
if (error instanceof HttpError) {
return {
isSuccess: false,
isFailure: true,
validationError: {},
message: error.getErrorMessage() ?? '',
};
}
return {
isSuccess: false,
isFailure: true,
validationError: {},
message: 'fail upload grade',
message: ERROR_CODE.INTERNAL_SEVER_ERROR,
};
}
redirect('/result');
};

export const parsePDFtoText = async (formData: FormData) => {
const res = await fetch(API_PATH.parsePDFtoText, { method: 'POST', body: formData });
if (!res.ok) {
return {
errors: {},
message: 'fail parsing to text',
};
}
return await res.text();
return (await fetch(API_PATH.parsePDFtoText, { method: 'POST', body: formData })).text();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Myongji-Graduate/fetch-ax#25

์ด๊ฑฐ text ํ•จ์ˆ˜ ํ•„์š” ์—†๋„๋ก ์ˆ˜์ •ํ•˜์‹  ๊ฑฐ ์•„๋‹Œ๊ฐ€์šฉ??

Copy link
Member Author

@yougyung yougyung Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ•ด๋‹น ์š”์ฒญ์ด PR์— ์„œ์ˆ ํ•œ formdata ์š”์ฒญ์— ๋Œ€ํ•œ ์˜ค๋ฅ˜๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.
๋”ฐ๋ผ fetch-ax์— ๋ณ€๊ฒฝ์‚ฌํ•ญ์ด ์—…๋ฐ์ดํŠธ ๋˜๊ธฐ ์ „๊นŒ์ง€, fetch-ax๋กœ ์ „ํ™˜ํ•˜๋Š” ๊ฒƒ์„ ๋ฏธ๋ฃจ๊ณ  fetch๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ์š”์ฒญ์€ ์‘๋‹ต์ด textํ˜•์‹์œผ๋กœ ์ „๋‹ฌ๋˜๋ฏ€๋กœ text()๋ฅผ ํ†ตํ•ด parsing์„ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค!

};

export const deleteTakenLecture = async (lectureId: number) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import { registerUserGrade } from '@/app/business/services/lecture/taken-lecture.command';
import UploadPdf from '@/app/ui/view/molecule/upload-pdf/upload-pdf';
import Form from '../../view/molecule/form';
import { FormState } from '../../view/molecule/form/form-root';
import { useRouter } from 'next/navigation';

function UploadTakenLecture() {
const router = useRouter();
const handleSuccess = (formState?: FormState) => {
if (formState?.isSuccess) router.push('/my');
};

return (
<Form action={registerUserGrade} id="์„ฑ์ ์—…๋กœ๋“œ">
<Form action={registerUserGrade} id="์„ฑ์ ์—…๋กœ๋“œ" onSuccess={handleSuccess}>
<UploadPdf />
<div className="py-6">
<Form.SubmitButton label="๊ฒฐ๊ณผ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ" position="center" size="md" />
Expand Down
6 changes: 3 additions & 3 deletions app/utils/api/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const ERROR_CODE = {
UNFITTED_GRADUATION_CATEGORY: '',

// LECTURE (์•„๋ž˜ ์„ธ๊ฐœ ๋ชจ๋‘ PDF Parsing์—์„œ ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ)
INCORRECT_STUDENT_NUMBER: '',
NON_EXISTED_LECTURE: '',
UNSUPPORTED_STUDENT_CATEGORY: '',
INCORRECT_STUDENT_NUMBER: 'ํ•™๋ฒˆ๊ณผ ์ผ์น˜ํ•˜๋Š” ์„ฑ์ PDF๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.',
NON_EXISTED_LECTURE: '์•Œ์ˆ˜์—†๋Š” ์ˆ˜๊ฐ•๊ณผ๋ชฉ์ด ์กด์žฌํ•ด์š”. ์ฑ„๋„ํ†ก์œผ๋กœ ๋ฌธ์˜ํ•ด์ฃผ์„ธ์š”',
UNSUPPORTED_STUDENT_CATEGORY: 'ํ˜„์žฌ ์ง€์›ํ•˜์ง€์•Š๋Š” ํ•™๋ฒˆ์ด์—์š”.',
} as const;
14 changes: 13 additions & 1 deletion app/utils/http/http-error.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { ERROR_CODE } from '../api/constant';

type ErrorConstrutor = {
message?: string;
statusCode?: number;
response?: Partial<Response>;
};

interface HttpResponse extends Response {
data: { errorCode: keyof typeof ERROR_CODE | string };
}

// Refactor: fetch๊ฐ€ NetworkError๋ž‘ TimeoutError ๋˜ ์ž๋™์œผ๋กœ ์—๋Ÿฌ๋กœ ๋˜์ ธ์„œ ์ฒ˜๋ฆฌ๊ฐ€ ๋” ์• ๋งคํ•˜๋‹ค.
// export class NetworkError extends Error {
// constructor(readonly message = 'Network Error') {
Expand All @@ -23,10 +29,16 @@ export class HttpError extends Error {
constructor(
readonly statusCode?: number,
message?: string,
readonly response?: Partial<Response>,
readonly response?: Partial<HttpResponse>,
) {
super(message);
}

getErrorMessage(): string {
const errorCode = this.response?.data?.errorCode;
if (!errorCode) return ERROR_CODE.INTERNAL_SEVER_ERROR;
return errorCode in ERROR_CODE ? ERROR_CODE[errorCode as keyof typeof ERROR_CODE] : errorCode;
}
Comment on lines +37 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Error ๊ฐ์ฒด์—์„œ ์—๋Ÿฌ ๋ฉ”์‹œ์ง€ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹ ์ข‹๋„ค์š”

}

export class BadRequestError extends HttpError {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
"express": "^4.18.2",
"fetch-ax": "^1.0.10",
"fetch-ax": "^2.0.3",
"jotai": "^2.7.0",
"lucide-react": "^0.336.0",
"next": "^14.2.13",
Expand Down
Loading