chore: 개발 중에는 콘텐츠 요청을 성공해도 찍는다
브라우저 네트워크 탭에는 백엔드 호출이 뜨지 않는다 — 화면은 Server Action을 부르고 호출은 서버에서 일어난다. 개발에서는 성공·실패를 가리지 않고 남기고, 운영에서는 실패만 남긴다. Co-Authored-By: Claude Opus 5
@61f004ae6f20f10abcd2e37c76651c926b6884fd
--- lib/data/repositories/content-repository.ts
+++ lib/data/repositories/content-repository.ts
... | ... | @@ -324,6 +324,47 @@ |
| 324 | 324 |
return params; |
| 325 | 325 |
} |
| 326 | 326 |
|
| 327 |
+/** |
|
| 328 |
+ * 보낸 것을 서버 콘솔에 그대로 남긴다. |
|
| 329 |
+ * |
|
| 330 |
+ * 브라우저 네트워크 탭에는 이 요청이 **뜨지 않는다** — 화면은 Server Action을 부르고 백엔드 |
|
| 331 |
+ * 호출은 서버에서 일어난다(BFF). 그래서 개발 중에는 성공·실패를 가리지 않고 찍고, 운영에서는 |
|
| 332 |
+ * 실패만 남긴다. JSON은 Swagger의 Request body에 그대로 붙여 넣을 수 있는 모양이다. |
|
| 333 |
+ */ |
|
| 334 |
+function logWrite( |
|
| 335 |
+ method: string, |
|
| 336 |
+ path: string, |
|
| 337 |
+ body: unknown, |
|
| 338 |
+ form: Record<string, string | number | undefined> | undefined, |
|
| 339 |
+ failure: { code: number; message: string } | null
|
|
| 340 |
+): void {
|
|
| 341 |
+ if (failure === null && process.env.NODE_ENV === 'production') {
|
|
| 342 |
+ return; |
|
| 343 |
+ } |
|
| 344 |
+ |
|
| 345 |
+ const head = failure |
|
| 346 |
+ ? `[contents] ${method} ${path} 실패 (code ${failure.code}: ${failure.message})`
|
|
| 347 |
+ : `[contents] ${method} ${path}`;
|
|
| 348 |
+ |
|
| 349 |
+ const lines = [ |
|
| 350 |
+ head, |
|
| 351 |
+ body === undefined ? null : `body(JSON):\n${JSON.stringify(body, null, 2)}`,
|
|
| 352 |
+ form === undefined |
|
| 353 |
+ ? null |
|
| 354 |
+ : `form:\n${new URLSearchParams(
|
|
| 355 |
+ Object.entries(form).flatMap(([key, value]) => |
|
| 356 |
+ value === undefined ? [] : [[key, String(value)] as [string, string]] |
|
| 357 |
+ ) |
|
| 358 |
+ ).toString()}`, |
|
| 359 |
+ ].filter(Boolean); |
|
| 360 |
+ |
|
| 361 |
+ if (failure) {
|
|
| 362 |
+ console.error(lines.join('\n'));
|
|
| 363 |
+ } else {
|
|
| 364 |
+ console.info(lines.join('\n'));
|
|
| 365 |
+ } |
|
| 366 |
+} |
|
| 367 |
+ |
|
| 327 | 368 |
async function sendWrite( |
| 328 | 369 |
path: string, |
| 329 | 370 |
method: 'POST' | 'PUT' | 'DELETE', |
... | ... | @@ -342,24 +383,9 @@ |
| 342 | 383 |
canHaveEmptyBody: true, |
| 343 | 384 |
}); |
| 344 | 385 |
|
| 386 |
+ logWrite(method, path, body, form, result.ok ? null : result); |
|
| 387 |
+ |
|
| 345 | 388 |
if (!result.ok) {
|
| 346 |
- // 백엔드 로그를 볼 수 없어, 무엇을 보냈는지 여기서 그대로 남긴다. JSON 본문은 Swagger의 |
|
| 347 |
- // Request body에 그대로 붙여 넣을 수 있는 모양으로 찍는다. |
|
| 348 |
- console.error( |
|
| 349 |
- [ |
|
| 350 |
- `[contents] ${method} ${path} 실패 (code ${result.code}: ${result.message})`,
|
|
| 351 |
- body === undefined ? null : `body(JSON):\n${JSON.stringify(body, null, 2)}`,
|
|
| 352 |
- form === undefined |
|
| 353 |
- ? null |
|
| 354 |
- : `form:\n${new URLSearchParams(
|
|
| 355 |
- Object.entries(form).flatMap(([key, value]) => |
|
| 356 |
- value === undefined ? [] : [[key, String(value)] as [string, string]] |
|
| 357 |
- ) |
|
| 358 |
- ).toString()}`, |
|
| 359 |
- ] |
|
| 360 |
- .filter(Boolean) |
|
| 361 |
- .join('\n')
|
|
| 362 |
- ); |
|
| 363 | 389 |
throw new BackendRequestError(result); |
| 364 | 390 |
} |
| 365 | 391 |
|
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?