Skip to content

Commit fadae97

Browse files
authored
Merge pull request #68 from classic-daramg/chore/user
๋‹‰๋„ค์ž„ ๊ฒ€์ฆ 400 ์‘๋‹ต ์ฒ˜๋ฆฌ, ํ”„๋กœํ•„ email ์ถ”๊ฐ€
2 parents 9b736e0 + cd203f0 commit fadae97

File tree

4 files changed

+38
-6
lines changed

4 files changed

+38
-6
lines changed

โ€Žsrc/main/java/com/daramg/server/common/exception/GlobalExceptionHandler.javaโ€Ž

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.daramg.server.common.exception;
22

33
import com.daramg.server.common.dto.ErrorResponse;
4+
import jakarta.validation.ConstraintViolationException;
45
import lombok.RequiredArgsConstructor;
56
import lombok.extern.slf4j.Slf4j;
67
import org.springframework.http.HttpHeaders;
@@ -87,6 +88,29 @@ protected ResponseEntity<Object> handleExceptionInternal(Exception ex, Object bo
8788
return super.handleExceptionInternal(ex, body, headers, statusCode, request);
8889
}
8990

91+
@ExceptionHandler(ConstraintViolationException.class)
92+
public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException e) {
93+
log.warn("ConstraintViolationException: {}", e.getMessage());
94+
95+
List<ErrorResponse.FieldErrorResponse> fieldErrors = e.getConstraintViolations().stream()
96+
.map(violation -> {
97+
String propertyPath = violation.getPropertyPath().toString();
98+
String field = propertyPath.contains(".")
99+
? propertyPath.substring(propertyPath.lastIndexOf('.') + 1)
100+
: propertyPath;
101+
return new ErrorResponse.FieldErrorResponse(
102+
field,
103+
violation.getInvalidValue() == null ? "" : violation.getInvalidValue().toString(),
104+
violation.getMessage()
105+
);
106+
})
107+
.collect(Collectors.toList());
108+
109+
return ResponseEntity
110+
.status(HttpStatus.BAD_REQUEST)
111+
.body(ErrorResponse.of(CommonErrorStatus.BAD_REQUEST, fieldErrors));
112+
}
113+
90114
@Override
91115
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException e, HttpHeaders headers, HttpStatusCode status, WebRequest request) {
92116
log.warn("MethodArgumentNotValidException: {}", e.getMessage());

โ€Žsrc/main/java/com/daramg/server/user/dto/UserProfileResponseDto.javaโ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
public record UserProfileResponseDto(
66
String profileImage,
77
String nickname,
8-
String bio
8+
String bio,
9+
String email
910
) {
1011
public static UserProfileResponseDto from(User user) {
1112
return new UserProfileResponseDto(
1213
user.getProfileImage(),
1314
user.getNickname(),
14-
user.getBio()
15+
user.getBio(),
16+
user.getEmail()
1517
);
1618
}
1719
}

โ€Žsrc/test/java/com/daramg/server/user/application/UserServiceTest.javaโ€Ž

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class ProfileTest {
5959
String profileImage = "https://example.com/profile.jpg";
6060
String nickname = "ํ…Œ์ŠคํŠธ๋‹‰๋„ค์ž„";
6161
String bio = "ํ…Œ์ŠคํŠธ ์†Œ๊ฐœ๊ธ€";
62-
User user = new User("test@email.com", "password", "ํ…Œ์ŠคํŠธ", LocalDate.now(), profileImage, nickname, bio, null);
62+
String email = "test@email.com";
63+
User user = new User(email, "password", "ํ…Œ์ŠคํŠธ", LocalDate.now(), profileImage, nickname, bio, null);
6364
userRepository.save(user);
6465

6566
//when
@@ -69,13 +70,15 @@ class ProfileTest {
6970
assertThat(result.profileImage()).isEqualTo(profileImage);
7071
assertThat(result.nickname()).isEqualTo(nickname);
7172
assertThat(result.bio()).isEqualTo(bio);
73+
assertThat(result.email()).isEqualTo(email);
7274
}
7375

7476
@Test
7577
void ํ”„๋กœํ•„_์ด๋ฏธ์ง€์™€_์†Œ๊ฐœ๊ธ€์ด_null์ธ_์œ ์ €์˜_ํ”„๋กœํ•„์„_์กฐํšŒํ•œ๋‹ค() {
7678
//given
7779
String nickname = "ํ…Œ์ŠคํŠธ๋‹‰๋„ค์ž„";
78-
User user = new User("test@email.com", "password", "ํ…Œ์ŠคํŠธ", LocalDate.now(), null, nickname, null, null);
80+
String email = "test@email.com";
81+
User user = new User(email, "password", "ํ…Œ์ŠคํŠธ", LocalDate.now(), null, nickname, null, null);
7982
userRepository.save(user);
8083

8184
//when
@@ -85,6 +88,7 @@ class ProfileTest {
8588
assertThat(result.profileImage()).isNull();
8689
assertThat(result.nickname()).isEqualTo(nickname);
8790
assertThat(result.bio()).isNull();
91+
assertThat(result.email()).isEqualTo(email);
8892
}
8993
}
9094

โ€Žsrc/test/java/com/daramg/server/user/presentation/UserControllerTest.javaโ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public class UserControllerTest extends ControllerTestSupport {
7373
String profileImage = "https://example.com/profile.jpg";
7474
String nickname = "ํ…Œ์ŠคํŠธ๋‹‰๋„ค์ž„";
7575
String bio = "ํ…Œ์ŠคํŠธ ์†Œ๊ฐœ๊ธ€";
76-
UserProfileResponseDto responseDto = new UserProfileResponseDto(profileImage, nickname, bio);
76+
String email = "test@email.com";
77+
UserProfileResponseDto responseDto = new UserProfileResponseDto(profileImage, nickname, bio, email);
7778
given(userService.getProfile(any(User.class))).willReturn(responseDto);
7879

7980
Cookie cookie = new Cookie(COOKIE_NAME, "access_token");
@@ -94,7 +95,8 @@ public class UserControllerTest extends ControllerTestSupport {
9495
.responseFields(
9596
fieldWithPath("profileImage").type(JsonFieldType.STRING).description("ํ”„๋กœํ•„ ์ด๋ฏธ์ง€ URL").optional(),
9697
fieldWithPath("nickname").type(JsonFieldType.STRING).description("๋‹‰๋„ค์ž„"),
97-
fieldWithPath("bio").type(JsonFieldType.STRING).description("์ž๊ธฐ์†Œ๊ฐœ").optional()
98+
fieldWithPath("bio").type(JsonFieldType.STRING).description("์ž๊ธฐ์†Œ๊ฐœ").optional(),
99+
fieldWithPath("email").type(JsonFieldType.STRING).description("์ด๋ฉ”์ผ")
98100
)
99101
.build()
100102
),

0 commit comments

Comments
ย (0)