fix: 콘텐츠 저장이 코드표 조회 실패에서 'stack' 오류로 끝나던 문제
학교·학년 코드표 조회(`fetchSchoolGradeCodes`)가 검증 인자로 들어가면서 try/catch **밖**에
놓였다. 저장 호출만 감싸고 있었으니 코드 조회가 실패하면 예외가 Server Action을 그대로 빠져나가
`Cannot read properties of undefined (reading 'stack')`로 끝난다.
실패는 실제로 일어난다 — 백엔드는 인가 실패도 200이 아닌 401에 봉투를 실어 돌려주고
(확인: `GET /api/v1/mngr/code/list/COM_SCHUL_CD` → `{"code":401,"message":"인가된 사용자가
아닙니다."}`), 세션 쿠키는 살아 있어도 액세스 토큰이 먼저 만료될 수 있다. 화면을 열 때는
멀쩡하다가 저장할 때 터지는 이유가 이것이다.
여섯 개 쓰기 액션의 본문을 `runWrite`로 감싸 코드표 조회·업로드·저장이 모두 한 울타리 안에서
일어나게 한다(게시판 액션과 같은 방식). `redirect()`는 예외로 흐름을 끊으므로 울타리 밖에 둔다.
이제 같은 실패가 폼 아래 메시지로 보인다.
함께 고친 것:
- 코드가 한 건도 없는 그룹이 `data: null`로 오면 예외 대신 빈 목록으로 다룬다. 학년(`GRD_CD`)은
학교에 따라 결과가 빌 수 있어 이것을 오류로 보면 화면 전체가 멎는다.
- `/contents`에 error·loading 경계를 둔다(다른 화면과 같은 구성). 없으면 조회 실패가 세그먼트
경계 없이 위로 튀어 같은 종류의 불투명한 오류로 보인다.
Co-Authored-By: Claude Opus 5
@20c270c7b381045240069955066a108315aec3f9
--- app/(protected)/(basic)/contents/_actions.ts
+++ app/(protected)/(basic)/contents/_actions.ts
... | ... | @@ -42,7 +42,7 @@ |
| 42 | 42 |
|
| 43 | 43 |
const SAVE_FAILED_MESSAGE = '저장하지 못했습니다. 잠시 후 다시 시도해 주세요.'; |
| 44 | 44 |
const DELETE_FAILED_MESSAGE = '삭제하지 못했습니다. 잠시 후 다시 시도해 주세요.'; |
| 45 |
-const UPLOAD_FAILED_MESSAGE = '파일을 업로드하지 못했습니다.'; |
|
| 45 |
+const INVALID_REQUEST_MESSAGE = '요청이 올바르지 않습니다.'; |
|
| 46 | 46 |
const VIDEO_UPLOADING_MESSAGE = |
| 47 | 47 |
'영상을 올리는 중입니다. 업로드가 끝난 뒤 저장해 주세요.'; |
| 48 | 48 |
const OVERSIZE_MESSAGE = `첨부파일은 ${Math.floor(MAX_ATTACHMENT_BYTES / (1024 * 1024))}MB까지 올릴 수 있습니다.`;
|
... | ... | @@ -100,6 +100,21 @@ |
| 100 | 100 |
return uploadAttachment(file, module.id); |
| 101 | 101 |
} |
| 102 | 102 |
|
| 103 |
+/** |
|
| 104 |
+ * 쓰기 동작 한 건을 감싼다 — 여기서 새는 예외가 하나라도 있으면 Next가 직렬화하지 못하고 |
|
| 105 |
+ * `Cannot read properties of undefined (reading 'stack')`로 끝난다. 코드표 조회·업로드·저장이 |
|
| 106 |
+ * 모두 이 안에서 일어나야 한다. `redirect()`는 예외로 흐름을 끊으므로 반드시 밖에서 부른다. |
|
| 107 |
+ */ |
|
| 108 |
+async function runWrite( |
|
| 109 |
+ run: () => Promise<ContentFormState | null> |
|
| 110 |
+): Promise<ContentFormState | null> {
|
|
| 111 |
+ try {
|
|
| 112 |
+ return await run(); |
|
| 113 |
+ } catch (error) {
|
|
| 114 |
+ return failure(error, SAVE_FAILED_MESSAGE); |
|
| 115 |
+ } |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 103 | 118 |
function failure(error: unknown, fallback: string): ContentFormState {
|
| 104 | 119 |
return {
|
| 105 | 120 |
status: 'error', |
... | ... | @@ -107,17 +122,8 @@ |
| 107 | 122 |
}; |
| 108 | 123 |
} |
| 109 | 124 |
|
| 110 |
-export async function createShortsAction( |
|
| 111 |
- _prevState: ContentFormState, |
|
| 112 |
- formData: FormData |
|
| 113 |
-): Promise<ContentFormState> {
|
|
| 114 |
- await verifySession(); |
|
| 115 |
- |
|
| 116 |
- if (readString(formData, 'videoUploading') === '1') {
|
|
| 117 |
- return { status: 'error', message: VIDEO_UPLOADING_MESSAGE };
|
|
| 118 |
- } |
|
| 119 |
- |
|
| 120 |
- const validation = validateShortsForm({
|
|
| 125 |
+function readShortsValues(formData: FormData) {
|
|
| 126 |
+ return {
|
|
| 121 | 127 |
schoolCode: readString(formData, 'schoolCode'), |
| 122 | 128 |
grades: readGrades(formData), |
| 123 | 129 |
title: readString(formData, 'title'), |
... | ... | @@ -127,80 +133,47 @@ |
| 127 | 133 |
videoFileId: readString(formData, 'videoFileId'), |
| 128 | 134 |
thumbnailFileId: readString(formData, 'thumbnailFileId'), |
| 129 | 135 |
isVisible: readString(formData, 'isVisible') === 'true', |
| 130 |
- }, await fetchSchoolGradeCodes()); |
|
| 131 |
- if (!validation.ok) {
|
|
| 132 |
- return { status: 'error', errors: validation.errors };
|
|
| 133 |
- } |
|
| 134 |
- |
|
| 135 |
- try {
|
|
| 136 |
- const values = {
|
|
| 137 |
- ...validation.values, |
|
| 138 |
- thumbnailFileId: await resolveFileId( |
|
| 139 |
- readFiles(formData, 'thumbnailFile')[0] ?? null, |
|
| 140 |
- validation.values.thumbnailFileId, |
|
| 141 |
- FILE_MODULE.contentThumbnail |
|
| 142 |
- ), |
|
| 143 |
- }; |
|
| 144 |
- await createContent( |
|
| 145 |
- 'shorts', |
|
| 146 |
- buildShortsPayload(values), |
|
| 147 |
- CONTENT_TYPE_CODE.shorts |
|
| 148 |
- ); |
|
| 149 |
- } catch (error) {
|
|
| 150 |
- return failure(error, SAVE_FAILED_MESSAGE); |
|
| 151 |
- } |
|
| 152 |
- |
|
| 153 |
- revalidatePath(contentListPath('shorts'));
|
|
| 154 |
- redirect(contentListPath('shorts'));
|
|
| 136 |
+ }; |
|
| 155 | 137 |
} |
| 156 | 138 |
|
| 157 |
-export async function updateShortsAction( |
|
| 158 |
- _prevState: ContentFormState, |
|
| 159 |
- formData: FormData |
|
| 160 |
-): Promise<ContentFormState> {
|
|
| 161 |
- await verifySession(); |
|
| 162 |
- |
|
| 163 |
- const cntntsSn = readString(formData, 'cntntsSn'); |
|
| 164 |
- if (readString(formData, 'videoUploading') === '1') {
|
|
| 165 |
- return { status: 'error', message: VIDEO_UPLOADING_MESSAGE };
|
|
| 166 |
- } |
|
| 167 |
- |
|
| 168 |
- const validation = validateShortsForm({
|
|
| 139 |
+function readComicValues(formData: FormData) {
|
|
| 140 |
+ return {
|
|
| 169 | 141 |
schoolCode: readString(formData, 'schoolCode'), |
| 170 | 142 |
grades: readGrades(formData), |
| 171 |
- title: readString(formData, 'title'), |
|
| 143 |
+ name: readString(formData, 'name'), |
|
| 172 | 144 |
subject: readString(formData, 'subject'), |
| 173 | 145 |
keywords: readKeywords(formData), |
| 174 | 146 |
message: readString(formData, 'message'), |
| 175 |
- videoFileId: readString(formData, 'videoFileId'), |
|
| 176 |
- thumbnailFileId: readString(formData, 'thumbnailFileId'), |
|
| 147 |
+ thumbnailFileId: '', |
|
| 148 |
+ cutFileIds: [] as string[], |
|
| 177 | 149 |
isVisible: readString(formData, 'isVisible') === 'true', |
| 178 |
- }, await fetchSchoolGradeCodes()); |
|
| 179 |
- if (!cntntsSn) {
|
|
| 180 |
- return { status: 'error', message: '요청이 올바르지 않습니다.' };
|
|
| 181 |
- } |
|
| 182 |
- if (!validation.ok) {
|
|
| 183 |
- return { status: 'error', errors: validation.errors };
|
|
| 184 |
- } |
|
| 185 |
- |
|
| 186 |
- try {
|
|
| 187 |
- const values = {
|
|
| 188 |
- ...validation.values, |
|
| 189 |
- thumbnailFileId: await resolveFileId( |
|
| 190 |
- readFiles(formData, 'thumbnailFile')[0] ?? null, |
|
| 191 |
- validation.values.thumbnailFileId, |
|
| 192 |
- FILE_MODULE.contentThumbnail |
|
| 193 |
- ), |
|
| 194 |
- }; |
|
| 195 |
- await updateContent('shorts', cntntsSn, buildShortsPayload(values));
|
|
| 196 |
- } catch (error) {
|
|
| 197 |
- return failure(error, SAVE_FAILED_MESSAGE); |
|
| 198 |
- } |
|
| 199 |
- |
|
| 200 |
- revalidatePath(contentListPath('shorts'));
|
|
| 201 |
- redirect(contentListPath('shorts'));
|
|
| 150 |
+ }; |
|
| 202 | 151 |
} |
| 203 | 152 |
|
| 153 |
+function readQuizValues(formData: FormData) {
|
|
| 154 |
+ return {
|
|
| 155 |
+ schoolCode: readString(formData, 'schoolCode'), |
|
| 156 |
+ grades: readGrades(formData), |
|
| 157 |
+ level: readString(formData, 'level'), |
|
| 158 |
+ keywords: readKeywords(formData), |
|
| 159 |
+ question: readString(formData, 'question'), |
|
| 160 |
+ answer: readString(formData, 'answer'), |
|
| 161 |
+ answerMessageCode: readString(formData, 'answerMessageCode'), |
|
| 162 |
+ answerMessageText: readString(formData, 'answerMessageText'), |
|
| 163 |
+ explanation: readString(formData, 'explanation'), |
|
| 164 |
+ isVisible: readString(formData, 'isVisible') === 'true', |
|
| 165 |
+ }; |
|
| 166 |
+} |
|
| 167 |
+ |
|
| 168 |
+async function resolveThumbnailId(formData: FormData): Promise<string> {
|
|
| 169 |
+ return resolveFileId( |
|
| 170 |
+ readFiles(formData, 'thumbnailFile')[0] ?? null, |
|
| 171 |
+ readString(formData, 'thumbnailFileId'), |
|
| 172 |
+ FILE_MODULE.contentThumbnail |
|
| 173 |
+ ); |
|
| 174 |
+} |
|
| 175 |
+ |
|
| 176 |
+/** 컷은 개별 이미지로 올린다(기획). 고르지 않은 칸은 폼이 들고 있던 기존 ID를 그대로 쓴다. */ |
|
| 204 | 177 |
async function resolveCutFileIds(formData: FormData): Promise<string[]> {
|
| 205 | 178 |
const files = readFiles(formData, 'cutFile'); |
| 206 | 179 |
const retained = formData |
... | ... | @@ -221,48 +194,112 @@ |
| 221 | 194 |
return ids; |
| 222 | 195 |
} |
| 223 | 196 |
|
| 197 |
+export async function createShortsAction( |
|
| 198 |
+ _prevState: ContentFormState, |
|
| 199 |
+ formData: FormData |
|
| 200 |
+): Promise<ContentFormState> {
|
|
| 201 |
+ await verifySession(); |
|
| 202 |
+ |
|
| 203 |
+ if (readString(formData, 'videoUploading') === '1') {
|
|
| 204 |
+ return { status: 'error', message: VIDEO_UPLOADING_MESSAGE };
|
|
| 205 |
+ } |
|
| 206 |
+ |
|
| 207 |
+ const failed = await runWrite(async () => {
|
|
| 208 |
+ const validation = validateShortsForm( |
|
| 209 |
+ readShortsValues(formData), |
|
| 210 |
+ await fetchSchoolGradeCodes() |
|
| 211 |
+ ); |
|
| 212 |
+ if (!validation.ok) {
|
|
| 213 |
+ return { status: 'error', errors: validation.errors };
|
|
| 214 |
+ } |
|
| 215 |
+ |
|
| 216 |
+ await createContent( |
|
| 217 |
+ 'shorts', |
|
| 218 |
+ buildShortsPayload({
|
|
| 219 |
+ ...validation.values, |
|
| 220 |
+ thumbnailFileId: await resolveThumbnailId(formData), |
|
| 221 |
+ }), |
|
| 222 |
+ CONTENT_TYPE_CODE.shorts |
|
| 223 |
+ ); |
|
| 224 |
+ return null; |
|
| 225 |
+ }); |
|
| 226 |
+ if (failed) {
|
|
| 227 |
+ return failed; |
|
| 228 |
+ } |
|
| 229 |
+ |
|
| 230 |
+ revalidatePath(contentListPath('shorts'));
|
|
| 231 |
+ redirect(contentListPath('shorts'));
|
|
| 232 |
+} |
|
| 233 |
+ |
|
| 234 |
+export async function updateShortsAction( |
|
| 235 |
+ _prevState: ContentFormState, |
|
| 236 |
+ formData: FormData |
|
| 237 |
+): Promise<ContentFormState> {
|
|
| 238 |
+ await verifySession(); |
|
| 239 |
+ |
|
| 240 |
+ const cntntsSn = readString(formData, 'cntntsSn'); |
|
| 241 |
+ if (!cntntsSn) {
|
|
| 242 |
+ return { status: 'error', message: INVALID_REQUEST_MESSAGE };
|
|
| 243 |
+ } |
|
| 244 |
+ if (readString(formData, 'videoUploading') === '1') {
|
|
| 245 |
+ return { status: 'error', message: VIDEO_UPLOADING_MESSAGE };
|
|
| 246 |
+ } |
|
| 247 |
+ |
|
| 248 |
+ const failed = await runWrite(async () => {
|
|
| 249 |
+ const validation = validateShortsForm( |
|
| 250 |
+ readShortsValues(formData), |
|
| 251 |
+ await fetchSchoolGradeCodes() |
|
| 252 |
+ ); |
|
| 253 |
+ if (!validation.ok) {
|
|
| 254 |
+ return { status: 'error', errors: validation.errors };
|
|
| 255 |
+ } |
|
| 256 |
+ |
|
| 257 |
+ await updateContent( |
|
| 258 |
+ 'shorts', |
|
| 259 |
+ cntntsSn, |
|
| 260 |
+ buildShortsPayload({
|
|
| 261 |
+ ...validation.values, |
|
| 262 |
+ thumbnailFileId: await resolveThumbnailId(formData), |
|
| 263 |
+ }) |
|
| 264 |
+ ); |
|
| 265 |
+ return null; |
|
| 266 |
+ }); |
|
| 267 |
+ if (failed) {
|
|
| 268 |
+ return failed; |
|
| 269 |
+ } |
|
| 270 |
+ |
|
| 271 |
+ revalidatePath(contentListPath('shorts'));
|
|
| 272 |
+ redirect(contentListPath('shorts'));
|
|
| 273 |
+} |
|
| 274 |
+ |
|
| 224 | 275 |
export async function createComicAction( |
| 225 | 276 |
_prevState: ContentFormState, |
| 226 | 277 |
formData: FormData |
| 227 | 278 |
): Promise<ContentFormState> {
|
| 228 | 279 |
await verifySession(); |
| 229 | 280 |
|
| 230 |
- let cutFileIds: string[]; |
|
| 231 |
- let thumbnailFileId: string; |
|
| 232 |
- try {
|
|
| 233 |
- cutFileIds = await resolveCutFileIds(formData); |
|
| 234 |
- thumbnailFileId = await resolveFileId( |
|
| 235 |
- readFiles(formData, 'thumbnailFile')[0] ?? null, |
|
| 236 |
- readString(formData, 'thumbnailFileId'), |
|
| 237 |
- FILE_MODULE.contentThumbnail |
|
| 281 |
+ const failed = await runWrite(async () => {
|
|
| 282 |
+ const validation = validateComicForm( |
|
| 283 |
+ {
|
|
| 284 |
+ ...readComicValues(formData), |
|
| 285 |
+ thumbnailFileId: await resolveThumbnailId(formData), |
|
| 286 |
+ cutFileIds: await resolveCutFileIds(formData), |
|
| 287 |
+ }, |
|
| 288 |
+ await fetchSchoolGradeCodes() |
|
| 238 | 289 |
); |
| 239 |
- } catch (error) {
|
|
| 240 |
- return failure(error, UPLOAD_FAILED_MESSAGE); |
|
| 241 |
- } |
|
| 290 |
+ if (!validation.ok) {
|
|
| 291 |
+ return { status: 'error', errors: validation.errors };
|
|
| 292 |
+ } |
|
| 242 | 293 |
|
| 243 |
- const validation = validateComicForm({
|
|
| 244 |
- schoolCode: readString(formData, 'schoolCode'), |
|
| 245 |
- grades: readGrades(formData), |
|
| 246 |
- name: readString(formData, 'name'), |
|
| 247 |
- subject: readString(formData, 'subject'), |
|
| 248 |
- keywords: readKeywords(formData), |
|
| 249 |
- message: readString(formData, 'message'), |
|
| 250 |
- thumbnailFileId, |
|
| 251 |
- cutFileIds, |
|
| 252 |
- isVisible: readString(formData, 'isVisible') === 'true', |
|
| 253 |
- }, await fetchSchoolGradeCodes()); |
|
| 254 |
- if (!validation.ok) {
|
|
| 255 |
- return { status: 'error', errors: validation.errors };
|
|
| 256 |
- } |
|
| 257 |
- |
|
| 258 |
- try {
|
|
| 259 | 294 |
await createContent( |
| 260 | 295 |
'comic', |
| 261 | 296 |
buildComicPayload(validation.values), |
| 262 | 297 |
CONTENT_TYPE_CODE.comic |
| 263 | 298 |
); |
| 264 |
- } catch (error) {
|
|
| 265 |
- return failure(error, SAVE_FAILED_MESSAGE); |
|
| 299 |
+ return null; |
|
| 300 |
+ }); |
|
| 301 |
+ if (failed) {
|
|
| 302 |
+ return failed; |
|
| 266 | 303 |
} |
| 267 | 304 |
|
| 268 | 305 |
revalidatePath(contentListPath('comic'));
|
... | ... | @@ -277,60 +314,31 @@ |
| 277 | 314 |
|
| 278 | 315 |
const cntntsSn = readString(formData, 'cntntsSn'); |
| 279 | 316 |
if (!cntntsSn) {
|
| 280 |
- return { status: 'error', message: '요청이 올바르지 않습니다.' };
|
|
| 317 |
+ return { status: 'error', message: INVALID_REQUEST_MESSAGE };
|
|
| 281 | 318 |
} |
| 282 | 319 |
|
| 283 |
- let cutFileIds: string[]; |
|
| 284 |
- let thumbnailFileId: string; |
|
| 285 |
- try {
|
|
| 286 |
- cutFileIds = await resolveCutFileIds(formData); |
|
| 287 |
- thumbnailFileId = await resolveFileId( |
|
| 288 |
- readFiles(formData, 'thumbnailFile')[0] ?? null, |
|
| 289 |
- readString(formData, 'thumbnailFileId'), |
|
| 290 |
- FILE_MODULE.contentThumbnail |
|
| 320 |
+ const failed = await runWrite(async () => {
|
|
| 321 |
+ const validation = validateComicForm( |
|
| 322 |
+ {
|
|
| 323 |
+ ...readComicValues(formData), |
|
| 324 |
+ thumbnailFileId: await resolveThumbnailId(formData), |
|
| 325 |
+ cutFileIds: await resolveCutFileIds(formData), |
|
| 326 |
+ }, |
|
| 327 |
+ await fetchSchoolGradeCodes() |
|
| 291 | 328 |
); |
| 292 |
- } catch (error) {
|
|
| 293 |
- return failure(error, UPLOAD_FAILED_MESSAGE); |
|
| 294 |
- } |
|
| 329 |
+ if (!validation.ok) {
|
|
| 330 |
+ return { status: 'error', errors: validation.errors };
|
|
| 331 |
+ } |
|
| 295 | 332 |
|
| 296 |
- const validation = validateComicForm({
|
|
| 297 |
- schoolCode: readString(formData, 'schoolCode'), |
|
| 298 |
- grades: readGrades(formData), |
|
| 299 |
- name: readString(formData, 'name'), |
|
| 300 |
- subject: readString(formData, 'subject'), |
|
| 301 |
- keywords: readKeywords(formData), |
|
| 302 |
- message: readString(formData, 'message'), |
|
| 303 |
- thumbnailFileId, |
|
| 304 |
- cutFileIds, |
|
| 305 |
- isVisible: readString(formData, 'isVisible') === 'true', |
|
| 306 |
- }, await fetchSchoolGradeCodes()); |
|
| 307 |
- if (!validation.ok) {
|
|
| 308 |
- return { status: 'error', errors: validation.errors };
|
|
| 309 |
- } |
|
| 310 |
- |
|
| 311 |
- try {
|
|
| 312 | 333 |
await updateContent('comic', cntntsSn, buildComicPayload(validation.values));
|
| 313 |
- } catch (error) {
|
|
| 314 |
- return failure(error, SAVE_FAILED_MESSAGE); |
|
| 334 |
+ return null; |
|
| 335 |
+ }); |
|
| 336 |
+ if (failed) {
|
|
| 337 |
+ return failed; |
|
| 315 | 338 |
} |
| 316 | 339 |
|
| 317 | 340 |
revalidatePath(contentListPath('comic'));
|
| 318 | 341 |
redirect(contentListPath('comic'));
|
| 319 |
-} |
|
| 320 |
- |
|
| 321 |
-function readQuizValues(formData: FormData) {
|
|
| 322 |
- return {
|
|
| 323 |
- schoolCode: readString(formData, 'schoolCode'), |
|
| 324 |
- grades: readGrades(formData), |
|
| 325 |
- level: readString(formData, 'level'), |
|
| 326 |
- keywords: readKeywords(formData), |
|
| 327 |
- question: readString(formData, 'question'), |
|
| 328 |
- answer: readString(formData, 'answer'), |
|
| 329 |
- answerMessageCode: readString(formData, 'answerMessageCode'), |
|
| 330 |
- answerMessageText: readString(formData, 'answerMessageText'), |
|
| 331 |
- explanation: readString(formData, 'explanation'), |
|
| 332 |
- isVisible: readString(formData, 'isVisible') === 'true', |
|
| 333 |
- }; |
|
| 334 | 342 |
} |
| 335 | 343 |
|
| 336 | 344 |
export async function createQuizAction( |
... | ... | @@ -339,15 +347,20 @@ |
| 339 | 347 |
): Promise<ContentFormState> {
|
| 340 | 348 |
await verifySession(); |
| 341 | 349 |
|
| 342 |
- const validation = validateQuizForm(readQuizValues(formData), await fetchSchoolGradeCodes()); |
|
| 343 |
- if (!validation.ok) {
|
|
| 344 |
- return { status: 'error', errors: validation.errors };
|
|
| 345 |
- } |
|
| 350 |
+ const failed = await runWrite(async () => {
|
|
| 351 |
+ const validation = validateQuizForm( |
|
| 352 |
+ readQuizValues(formData), |
|
| 353 |
+ await fetchSchoolGradeCodes() |
|
| 354 |
+ ); |
|
| 355 |
+ if (!validation.ok) {
|
|
| 356 |
+ return { status: 'error', errors: validation.errors };
|
|
| 357 |
+ } |
|
| 346 | 358 |
|
| 347 |
- try {
|
|
| 348 | 359 |
await createQuiz(validation.values); |
| 349 |
- } catch (error) {
|
|
| 350 |
- return failure(error, SAVE_FAILED_MESSAGE); |
|
| 360 |
+ return null; |
|
| 361 |
+ }); |
|
| 362 |
+ if (failed) {
|
|
| 363 |
+ return failed; |
|
| 351 | 364 |
} |
| 352 | 365 |
|
| 353 | 366 |
revalidatePath(contentListPath('quiz'));
|
... | ... | @@ -362,18 +375,23 @@ |
| 362 | 375 |
|
| 363 | 376 |
const quizSn = readString(formData, 'quizSn'); |
| 364 | 377 |
if (!quizSn) {
|
| 365 |
- return { status: 'error', message: '요청이 올바르지 않습니다.' };
|
|
| 378 |
+ return { status: 'error', message: INVALID_REQUEST_MESSAGE };
|
|
| 366 | 379 |
} |
| 367 | 380 |
|
| 368 |
- const validation = validateQuizForm(readQuizValues(formData), await fetchSchoolGradeCodes()); |
|
| 369 |
- if (!validation.ok) {
|
|
| 370 |
- return { status: 'error', errors: validation.errors };
|
|
| 371 |
- } |
|
| 381 |
+ const failed = await runWrite(async () => {
|
|
| 382 |
+ const validation = validateQuizForm( |
|
| 383 |
+ readQuizValues(formData), |
|
| 384 |
+ await fetchSchoolGradeCodes() |
|
| 385 |
+ ); |
|
| 386 |
+ if (!validation.ok) {
|
|
| 387 |
+ return { status: 'error', errors: validation.errors };
|
|
| 388 |
+ } |
|
| 372 | 389 |
|
| 373 |
- try {
|
|
| 374 | 390 |
await updateQuiz(quizSn, validation.values); |
| 375 |
- } catch (error) {
|
|
| 376 |
- return failure(error, SAVE_FAILED_MESSAGE); |
|
| 391 |
+ return null; |
|
| 392 |
+ }); |
|
| 393 |
+ if (failed) {
|
|
| 394 |
+ return failed; |
|
| 377 | 395 |
} |
| 378 | 396 |
|
| 379 | 397 |
revalidatePath(contentListPath('quiz'));
|
... | ... | @@ -387,7 +405,7 @@ |
| 387 | 405 |
await verifySession(); |
| 388 | 406 |
|
| 389 | 407 |
if (!id) {
|
| 390 |
- return { status: 'error', message: '요청이 올바르지 않습니다.' };
|
|
| 408 |
+ return { status: 'error', message: INVALID_REQUEST_MESSAGE };
|
|
| 391 | 409 |
} |
| 392 | 410 |
|
| 393 | 411 |
try {
|
+++ app/(protected)/(basic)/contents/error.tsx
... | ... | @@ -0,0 +1,32 @@ |
| 1 | +'use client'; | |
| 2 | + | |
| 3 | +import { useEffect } from 'react'; | |
| 4 | + | |
| 5 | +interface ErrorPageProps { | |
| 6 | + error: Error & { digest?: string }; | |
| 7 | + unstable_retry: () => void; | |
| 8 | +} | |
| 9 | + | |
| 10 | +// 게시판 세 화면이 공유하는 에러 경계. 원본 메시지·스택은 화면에 내지 않는다 — 개발자 진단용으로만 | |
| 11 | +// 콘솔에 남기고(프로덕션에서는 Next.js가 Server Component 에러를 이미 digest 포함 일반화 메시지로 | |
| 12 | +// 치환해 전달한다), 사용자에게는 일반화된 문구 + 재시도만 제공한다. Next.js 16.2 문서 권고에 따라 | |
| 13 | +// 세그먼트 재조회까지 수행하는 `unstable_retry()`를 `reset()` 대신 사용한다. | |
| 14 | +export default function ErrorPage({ error, unstable_retry }: ErrorPageProps) { | |
| 15 | + useEffect(() => { | |
| 16 | + console.error(error); | |
| 17 | + }, [error]); | |
| 18 | + | |
| 19 | + return ( | |
| 20 | + <section className="flex flex-1 flex-col items-center justify-center gap-2 p-8"> | |
| 21 | + <h1 className="text-lg text-foreground">문제가 발생했습니다.</h1> | |
| 22 | + <p className="text-sm text-foreground/60">잠시 후 다시 시도해 주세요.</p> | |
| 23 | + <button | |
| 24 | + type="button" | |
| 25 | + onClick={() => unstable_retry()} | |
| 26 | + className="mt-4 bg-foreground px-4 py-2 text-background" | |
| 27 | + > | |
| 28 | + 다시 시도 | |
| 29 | + </button> | |
| 30 | + </section> | |
| 31 | + ); | |
| 32 | +} |
+++ app/(protected)/(basic)/contents/loading.tsx
... | ... | @@ -0,0 +1,6 @@ |
| 1 | +import { PageLoading } from '@/components/ui/page-loading'; | |
| 2 | + | |
| 3 | +// page.tsx가 스트리밍되는 동안의 폴백. 네 화면이 같은 모양이라 공용 컴포넌트를 쓴다. | |
| 4 | +export default function Loading() { | |
| 5 | + return <PageLoading />; | |
| 6 | +} |
--- lib/data/repositories/common-code-repository.ts
+++ lib/data/repositories/common-code-repository.ts
... | ... | @@ -66,12 +66,20 @@ |
| 66 | 66 |
method: 'GET', |
| 67 | 67 |
accessToken: accessToken ?? undefined, |
| 68 | 68 |
cache: 'no-store', |
| 69 |
+ canHaveNullData: true, |
|
| 70 |
+ canHaveEmptyBody: true, |
|
| 69 | 71 |
}); |
| 70 | 72 |
|
| 71 | 73 |
if (!result.ok) {
|
| 72 | 74 |
throw new BackendRequestError(result); |
| 73 | 75 |
} |
| 74 | 76 |
|
| 77 |
+ // 코드가 한 건도 없는 그룹은 빈 목록으로 다룬다 — 학년(`GRD_CD`)처럼 상위 코드에 따라 |
|
| 78 |
+ // 결과가 비는 조회가 있어 이것을 오류로 보면 화면 전체가 멎는다. |
|
| 79 |
+ if (result.data === null || result.data === undefined) {
|
|
| 80 |
+ return []; |
|
| 81 |
+ } |
|
| 82 |
+ |
|
| 75 | 83 |
if (!Array.isArray(result.data)) {
|
| 76 | 84 |
throw new Error('공통코드 응답의 형식이 올바르지 않습니다.');
|
| 77 | 85 |
} |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?