Skip to content

Commit d01d29a

Browse files
committed
refactor: 환경 설정 개선 및 예외 처리 리팩토링
- application.yml | active : DB 환경변수 지정, info : 환경 변수로 DB 표시 - application-dev.yml | H2 MODE=PostgreSQL 설정 추가 - GlobalExceptionHandler 보완(전체 예외 처리 관련) 및 ErrorCode 추가(입력값 유효성 검증 관련) - mockito 의존성 제거
1 parent 5de833e commit d01d29a

File tree

7 files changed

+70
-22
lines changed

7 files changed

+70
-22
lines changed

build.gradle

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ dependencies {
6262
// Spring Boot Test 의존성
6363
testImplementation 'org.springframework.boot:spring-boot-starter-test'
6464

65-
// Mockito 의존성
66-
implementation "org.mockito:mockito-core:4.6.1"
67-
6865
// JUnit 플랫폼 런처 (JUnit 테스트 실행)
6966
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
7067

src/main/java/com/sprint/mission/discodeit/exception/ErrorCode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
@Getter
77
public enum ErrorCode {
8+
9+
// Validation 에러 추가
10+
VALIDATION_FAILED(HttpStatus.NOT_FOUND, "입력값 검증에 실패했습니다."),
11+
812
// 유저
913
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "유저를 찾을 수 없습니다."), // 404
1014
USERNAME_ALREADY_EXISTS(HttpStatus.CONFLICT, "이미 존재하는 사용자 이름입니다."), // 409

src/main/java/com/sprint/mission/discodeit/exception/GlobalExceptionHandler.java

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.time.Instant;
55
import java.util.HashMap;
66
import java.util.Map;
7+
import org.springframework.expression.spel.ast.NullLiteral;
78
import org.springframework.http.HttpStatus;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -12,10 +13,17 @@
1213
import java.time.LocalDateTime;
1314
import java.util.NoSuchElementException;
1415
import org.springframework.web.bind.annotation.RestControllerAdvice;
16+
import org.springframework.web.servlet.View;
1517

1618
@RestControllerAdvice
1719
public class GlobalExceptionHandler {
1820

21+
private final View error;
22+
23+
public GlobalExceptionHandler(View error) {
24+
this.error = error;
25+
}
26+
1927
// DiscodeitException 을 처리하는 핸들러
2028
@ExceptionHandler(DiscodeitException.class)
2129
public ResponseEntity<ErrorResponse> handleDiscodeitException(DiscodeitException ex) {
@@ -44,7 +52,7 @@ public ResponseEntity<ErrorResponse> handleDiscodeitException(DiscodeitException
4452
"status": 400
4553
}
4654
*/
47-
// MethodArgumentNotValidException 을 처리하는 핸들러
55+
// MethodArgumentNotValidException (Spring 입력 값 검증 예외) 을 처리하는 핸들러
4856
@ExceptionHandler(MethodArgumentNotValidException.class)
4957
public ResponseEntity<ErrorResponse> handleValidationExceptions(
5058
MethodArgumentNotValidException ex) {
@@ -57,13 +65,58 @@ public ResponseEntity<ErrorResponse> handleValidationExceptions(
5765

5866
ErrorResponse errorResponse = new ErrorResponse(
5967
Instant.now(),
60-
"VALIDATION_FAILED", // ErrorCoud Enum 의 코드명과 동일하게
61-
"입력값이 유효하지 않습니다.",
68+
ErrorCode.VALIDATION_FAILED.name(),
69+
ErrorCode.VALIDATION_FAILED.getMessage(),
6270
errors,
6371
ex.getClass().getSimpleName(), // 예외 클래스 이름
6472
HttpStatus.BAD_REQUEST.value()
6573
);
6674
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
6775
}
6876

77+
78+
// NullPointerException 를 처리하는 핸들러
79+
@ExceptionHandler(NullPointerException.class)
80+
public ResponseEntity<ErrorResponse> handleNullPointerException(NullPointerException ex) {
81+
ErrorResponse errorResponse = new ErrorResponse(
82+
Instant.now(),
83+
"NULL_POINTER_EXCEPTION",
84+
"예상치 못한 null이 발생했습니다.",
85+
null,
86+
ex.getClass().getSimpleName(),
87+
HttpStatus.INTERNAL_SERVER_ERROR.value()
88+
);
89+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
90+
}
91+
92+
// IllegalArgumentException 를 처리하는 핸들러
93+
@ExceptionHandler(IllegalArgumentException.class)
94+
public ResponseEntity<ErrorResponse> handleIllegalArgumentException(IllegalArgumentException ex) {
95+
ErrorResponse errorResponse = new ErrorResponse(
96+
Instant.now(),
97+
"ILLEGAL_ARGUMENT_EXCEPTION",
98+
"허용되지 않은 값입니다.",
99+
null,
100+
ex.getClass().getSimpleName(),
101+
HttpStatus.BAD_REQUEST.value()
102+
);
103+
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
104+
}
105+
106+
// 모든 예외를 처리하는 핸들러
107+
@ExceptionHandler({Exception.class, RuntimeException.class})
108+
public ResponseEntity<ErrorResponse> handleGeneralExceptions(Exception ex) {
109+
110+
ErrorResponse errorResponse = new ErrorResponse(
111+
Instant.now(),
112+
"UNKNOWN_ERROR",
113+
"알 수 없는 오류가 발생했습니다.",
114+
null,
115+
ex.getClass().getSimpleName(),
116+
HttpStatus.INTERNAL_SERVER_ERROR.value()
117+
);
118+
119+
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
120+
}
121+
69122
}

src/main/resources/application-dev.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
spring:
22
datasource:
3-
url: jdbc:h2:mem:devDB
3+
url: jdbc:h2:mem:devDB;MODE=PostgreSQL # PostgreSQL 호환 모드 추가
44
username: devUser
55
password: devPass
66
driver-class-name: org.h2.Driver # 명시적으로 드라이버 클래스 지정

src/main/resources/application.yaml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ spring:
88
jpa:
99
show-sql: true
1010

11-
1211
servlet:
1312
multipart:
1413
max-file-size: 10MB # 파일 하나의 최대 크기
15-
max-request-size: 20MB #벙네 최대 업로드 가능 용량
14+
max-request-size: 20MB #번에 최대 업로드 가능 용량
1615
profiles:
17-
active: test
16+
active: ${SPRING_PROFILES_ACTIVE:test}
1817

1918
discodeit:
2019
storage:
@@ -56,12 +55,9 @@ info:
5655
java-version: 17
5756
spring-boot-version: 3.4.0
5857

59-
jpa:
60-
ddl-auto: validate
61-
62-
multipart:
63-
max-file-size: 10MB
64-
max-request-size: 20MB
58+
datasource:
59+
url: ${spring.datasource.url}
60+
driver-class-name: ${spring.datasource.driver-class-name}
6561

6662

6763
springdoc:

src/test/java/com/sprint/mission/discodeit/repository/MessageRepositoryTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,18 @@
1212
import org.junit.jupiter.api.BeforeEach;
1313
import org.junit.jupiter.api.DisplayName;
1414
import org.junit.jupiter.api.Test;
15-
import org.junit.jupiter.api.extension.ExtendWith;
1615
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
1717
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
1818
import org.springframework.context.annotation.Import;
1919
import org.springframework.data.domain.Sort;
20-
import org.springframework.test.context.junit.jupiter.SpringExtension;
2120

2221
import org.springframework.data.domain.Page;
2322
import org.springframework.data.domain.PageRequest;
2423
import org.springframework.data.domain.Pageable;
2524

2625
@Import(JpaConfig.class)
27-
@ExtendWith(SpringExtension.class) // Junit5 + Spring
26+
@AutoConfigureTestDatabase
2827
@DataJpaTest
2928
public class MessageRepositoryTest {
3029

src/test/java/com/sprint/mission/discodeit/repository/UserRepositoryTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
import com.sprint.mission.discodeit.entity.User;
77
import org.junit.jupiter.api.DisplayName;
88
import org.junit.jupiter.api.Test;
9-
import org.junit.jupiter.api.extension.ExtendWith;
109
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
1111
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
1212
import org.springframework.context.annotation.Import;
13-
import org.springframework.test.context.junit.jupiter.SpringExtension;
1413

1514
@Import(JpaConfig.class)
16-
@ExtendWith(SpringExtension.class) // Junit5 + Spring
15+
@AutoConfigureTestDatabase
1716
@DataJpaTest // JPA 와 연관된 Bean 만 로딩
1817
public class UserRepositoryTest {
1918

0 commit comments

Comments
 (0)