Skip to content

Commit a8a6ab3

Browse files
committed
fix: 공지사항 서버 에러 해결
1 parent 348774f commit a8a6ab3

27 files changed

Lines changed: 219 additions & 25 deletions

src/main/java/com/example/demo/config/PerformanceLoggingInterceptor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ public void afterCompletion(HttpServletRequest request, HttpServletResponse resp
6565

6666

6767

68+
6869

6970

src/main/java/com/example/demo/controller/ScheduleController.java

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.swagger.v3.oas.annotations.tags.Tag;
1919
import jakarta.validation.Valid;
2020
import lombok.RequiredArgsConstructor;
21+
import lombok.extern.slf4j.Slf4j;
2122
import org.springframework.format.annotation.DateTimeFormat;
2223
import org.springframework.http.HttpStatus;
2324
import org.springframework.http.ResponseEntity;
@@ -26,6 +27,7 @@
2627
import java.time.LocalDateTime;
2728
import java.util.List;
2829

30+
@Slf4j
2931
@Tag(name = "스케줄", description = "팀 스케줄 생성/수정/삭제 및 조회 API")
3032
@RestController
3133
@RequestMapping("/api/v1")
@@ -116,7 +118,11 @@ public ResponseEntity<?> updateSchedule(
116118
}
117119
ScheduleResponseDto response = scheduleService.updateSchedule(userId, scheduleId, request, scope);
118120
return ResponseEntity.ok(response);
121+
} catch (IllegalArgumentException e) {
122+
return handleIllegalArgumentException(e);
119123
} catch (Exception e) {
124+
log.error("스케줄 수정 중 예상치 못한 오류 발생: scheduleId={}, userId={}, error={}",
125+
scheduleId, jwtHelper.getCurrentUserId(), e.getMessage(), e);
120126
return createErrorResponse("스케줄 수정 중 오류가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
121127
}
122128
}
@@ -242,15 +248,76 @@ public ResponseEntity<?> getMySchedules(
242248
}
243249
}
244250

245-
private ResponseEntity<ErrorResponse> createUnauthorizedResponse(String message) {
246-
ErrorResponse error = new ErrorResponse("UNAUTHORIZED", message);
247-
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(error);
251+
/**
252+
* IllegalArgumentException 처리 (권한, 리소스 없음 등을 구분)
253+
*/
254+
private ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
255+
String message = e.getMessage();
256+
log.debug("IllegalArgumentException 발생: {}", message);
257+
258+
// 에러 코드가 포함된 경우 (예: "FORBIDDEN: ...", "SCHEDULE_NOT_FOUND: ...")
259+
if (message != null && message.contains(":")) {
260+
String errorCode = message.split(":")[0].trim();
261+
String cleanMessage = message.split(":", 2)[1].trim();
262+
263+
HttpStatus status;
264+
if ("FORBIDDEN".equals(errorCode)) {
265+
status = HttpStatus.FORBIDDEN;
266+
} else if ("SCHEDULE_NOT_FOUND".equals(errorCode) || "TEAM_NOT_FOUND".equals(errorCode)) {
267+
status = HttpStatus.NOT_FOUND;
268+
} else {
269+
status = HttpStatus.BAD_REQUEST;
270+
}
271+
272+
return ResponseEntity.status(status)
273+
.body(ErrorResponse.builder()
274+
.code(errorCode)
275+
.message(cleanMessage)
276+
.build());
277+
}
278+
279+
// 에러 코드가 없는 경우 메시지로 판단
280+
String code = "INVALID_REQUEST";
281+
HttpStatus status = HttpStatus.BAD_REQUEST;
282+
283+
if (message != null) {
284+
if (message.contains("권한") || message.contains("FORBIDDEN") || message.contains("팀원이 아닌")) {
285+
code = "FORBIDDEN";
286+
status = HttpStatus.FORBIDDEN;
287+
} else if (message.contains("찾을 수 없습니다") || message.contains("NOT_FOUND")) {
288+
code = "SCHEDULE_NOT_FOUND";
289+
status = HttpStatus.NOT_FOUND;
290+
}
291+
}
292+
293+
return ResponseEntity.status(status)
294+
.body(ErrorResponse.builder()
295+
.code(code)
296+
.message(message != null ? message : "잘못된 요청입니다.")
297+
.build());
248298
}
249-
250-
299+
300+
/**
301+
* 에러 응답 생성
302+
*/
251303
private ResponseEntity<ErrorResponse> createErrorResponse(String message, HttpStatus status) {
252-
ErrorResponse error = new ErrorResponse("INTERNAL_SERVER_ERROR", message);
253-
return ResponseEntity.status(status).body(error);
304+
String code = status == HttpStatus.INTERNAL_SERVER_ERROR ? "INTERNAL_SERVER_ERROR" : "INVALID_REQUEST";
305+
return ResponseEntity.status(status)
306+
.body(ErrorResponse.builder()
307+
.code(code)
308+
.message(message)
309+
.build());
310+
}
311+
312+
/**
313+
* 인증 실패 응답 생성
314+
*/
315+
private ResponseEntity<ErrorResponse> createUnauthorizedResponse(String message) {
316+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
317+
.body(ErrorResponse.builder()
318+
.code("UNAUTHORIZED")
319+
.message(message)
320+
.build());
254321
}
255322
}
256323

src/main/java/com/example/demo/controller/TodoController.java

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import io.swagger.v3.oas.annotations.tags.Tag;
1919
import jakarta.validation.Valid;
2020
import lombok.RequiredArgsConstructor;
21+
import lombok.extern.slf4j.Slf4j;
2122
import org.springframework.format.annotation.DateTimeFormat;
2223
import org.springframework.http.HttpStatus;
2324
import org.springframework.http.ResponseEntity;
@@ -26,6 +27,7 @@
2627
import java.time.LocalDateTime;
2728
import java.util.List;
2829

30+
@Slf4j
2931
@Tag(name = "투두", description = "팀 투두 생성/수정/삭제 및 조회 API")
3032
@RestController
3133
@RequestMapping("/api/v1")
@@ -114,7 +116,11 @@ public ResponseEntity<?> updateTodo(
114116
}
115117
TodoResponseDto response = todoService.updateTodo(userId, todoId, request);
116118
return ResponseEntity.ok(response);
119+
} catch (IllegalArgumentException e) {
120+
return handleIllegalArgumentException(e);
117121
} catch (Exception e) {
122+
log.error("투두 수정 중 예상치 못한 오류 발생: todoId={}, userId={}, error={}",
123+
todoId, jwtHelper.getCurrentUserId(), e.getMessage(), e);
118124
return createErrorResponse("투두 수정 중 오류가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
119125
}
120126
}
@@ -158,14 +164,10 @@ public ResponseEntity<?> updateTodoStatus(
158164
TodoResponseDto response = todoService.updateTodoStatus(userId, todoId, request);
159165
return ResponseEntity.ok(response);
160166
} catch (IllegalArgumentException e) {
161-
String message = e.getMessage();
162-
if (message != null && message.contains("투두를 찾을 수 없습니다")) {
163-
return createErrorResponse("TODO_NOT_FOUND", message, HttpStatus.NOT_FOUND);
164-
} else if (message != null && message.contains("팀원이 아닌")) {
165-
return createErrorResponse("FORBIDDEN", message, HttpStatus.FORBIDDEN);
166-
}
167-
return createErrorResponse("투두 완료 여부 수정 중 오류가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
167+
return handleIllegalArgumentException(e);
168168
} catch (Exception e) {
169+
log.error("투두 완료 여부 수정 중 예상치 못한 오류 발생: todoId={}, userId={}, error={}",
170+
todoId, jwtHelper.getCurrentUserId(), e.getMessage(), e);
169171
return createErrorResponse("투두 완료 여부 수정 중 오류가 발생했습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
170172
}
171173
}
@@ -283,20 +285,76 @@ public ResponseEntity<?> getMyTodos(
283285
}
284286
}
285287

286-
private ResponseEntity<ErrorResponse> createUnauthorizedResponse(String message) {
287-
ErrorResponse error = new ErrorResponse("UNAUTHORIZED", message);
288-
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(error);
288+
/**
289+
* IllegalArgumentException 처리 (권한, 리소스 없음 등을 구분)
290+
*/
291+
private ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException e) {
292+
String message = e.getMessage();
293+
log.debug("IllegalArgumentException 발생: {}", message);
294+
295+
// 에러 코드가 포함된 경우 (예: "FORBIDDEN: ...", "TODO_NOT_FOUND: ...")
296+
if (message != null && message.contains(":")) {
297+
String errorCode = message.split(":")[0].trim();
298+
String cleanMessage = message.split(":", 2)[1].trim();
299+
300+
HttpStatus status;
301+
if ("FORBIDDEN".equals(errorCode)) {
302+
status = HttpStatus.FORBIDDEN;
303+
} else if ("TODO_NOT_FOUND".equals(errorCode) || "TEAM_NOT_FOUND".equals(errorCode)) {
304+
status = HttpStatus.NOT_FOUND;
305+
} else {
306+
status = HttpStatus.BAD_REQUEST;
307+
}
308+
309+
return ResponseEntity.status(status)
310+
.body(ErrorResponse.builder()
311+
.code(errorCode)
312+
.message(cleanMessage)
313+
.build());
314+
}
315+
316+
// 에러 코드가 없는 경우 메시지로 판단
317+
String code = "INVALID_REQUEST";
318+
HttpStatus status = HttpStatus.BAD_REQUEST;
319+
320+
if (message != null) {
321+
if (message.contains("권한") || message.contains("FORBIDDEN") || message.contains("팀원이 아닌")) {
322+
code = "FORBIDDEN";
323+
status = HttpStatus.FORBIDDEN;
324+
} else if (message.contains("찾을 수 없습니다") || message.contains("NOT_FOUND")) {
325+
code = "TODO_NOT_FOUND";
326+
status = HttpStatus.NOT_FOUND;
327+
}
328+
}
329+
330+
return ResponseEntity.status(status)
331+
.body(ErrorResponse.builder()
332+
.code(code)
333+
.message(message != null ? message : "잘못된 요청입니다.")
334+
.build());
289335
}
290-
291-
336+
337+
/**
338+
* 에러 응답 생성
339+
*/
292340
private ResponseEntity<ErrorResponse> createErrorResponse(String message, HttpStatus status) {
293-
ErrorResponse error = new ErrorResponse("INTERNAL_SERVER_ERROR", message);
294-
return ResponseEntity.status(status).body(error);
341+
String code = status == HttpStatus.INTERNAL_SERVER_ERROR ? "INTERNAL_SERVER_ERROR" : "INVALID_REQUEST";
342+
return ResponseEntity.status(status)
343+
.body(ErrorResponse.builder()
344+
.code(code)
345+
.message(message)
346+
.build());
295347
}
296-
297-
private ResponseEntity<ErrorResponse> createErrorResponse(String code, String message, HttpStatus status) {
298-
ErrorResponse error = new ErrorResponse(code, message);
299-
return ResponseEntity.status(status).body(error);
348+
349+
/**
350+
* 인증 실패 응답 생성
351+
*/
352+
private ResponseEntity<ErrorResponse> createUnauthorizedResponse(String message) {
353+
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
354+
.body(ErrorResponse.builder()
355+
.code("UNAUTHORIZED")
356+
.message(message)
357+
.build());
300358
}
301359
}
302360

src/main/java/com/example/demo/domain/entity/ScheduleAttendee.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ public class ScheduleAttendee {
5959

6060

6161

62+
6263

6364

src/main/java/com/example/demo/domain/entity/ScheduleAttendeeId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public class ScheduleAttendeeId implements Serializable {
4949

5050

5151

52+
5253

5354

src/main/java/com/example/demo/domain/entity/SchedulePosition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ public class SchedulePosition {
6262

6363

6464

65+
6566

6667

src/main/java/com/example/demo/domain/entity/SchedulePositionId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public class SchedulePositionId implements Serializable {
4949

5050

5151

52+
5253

5354

src/main/java/com/example/demo/domain/entity/TodoAttendee.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ public class TodoAttendee {
5959

6060

6161

62+
6263

6364

src/main/java/com/example/demo/domain/entity/TodoAttendeeId.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ public class TodoAttendeeId implements Serializable {
4949

5050

5151

52+
5253

5354

src/main/java/com/example/demo/domain/entity/TodoPosition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,6 @@ public class TodoPosition {
6262

6363

6464

65+
6566

6667

0 commit comments

Comments
 (0)