Skip to content

Commit 9d36208

Browse files
authored
Merge pull request #22 from Block-Guard/fix/#20/image-upload-size-exception
[Fix] 이미지 업로드 용량 초과 예외 처리 & 보호자 이름 유니크 제약 및 예외 처리
2 parents 567d3d5 + 7cee922 commit 9d36208

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

src/main/java/com/blockguard/server/domain/guardian/application/GuardianService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public GuardiansListResponse getGuardiansList(User user) {
3434

3535
@Transactional
3636
public GuardianResponse createGuardian(User user, CreateGuardianRequest request) {
37+
if (guardianRepository.existsByUserAndNameAndDeletedAtIsNull(user, request.getName())) {
38+
throw new BusinessExceptionHandler(ErrorCode.DUPLICATE_GUARDIAN_NAME);
39+
}
40+
3741
String key = (request.getProfileImage() != null && !request.getProfileImage().isEmpty())
3842
? s3Service.upload(request.getProfileImage(), "guardians"): null;
3943

@@ -54,6 +58,11 @@ public GuardianResponse updateGuardian(User user, Long guardianId, CreateGuardia
5458
Guardian guardian = guardianRepository.findByIdAndUser(guardianId, user)
5559
.orElseThrow(() -> new BusinessExceptionHandler(ErrorCode.GUARDIAN_NOT_FOUND));
5660

61+
if (!guardian.getName().equals(request.getName()) &&
62+
guardianRepository.existsByUserAndNameAndDeletedAtIsNull(user, request.getName())) {
63+
throw new BusinessExceptionHandler(ErrorCode.DUPLICATE_GUARDIAN_NAME);
64+
}
65+
5766
String key = null;
5867
if (request.getProfileImage() != null && !request.getProfileImage().isEmpty()){
5968
// 새 이미지 업로드

src/main/java/com/blockguard/server/domain/guardian/dao/GuardianRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ public interface GuardianRepository extends JpaRepository<Guardian, Long> {
2222
AND g.isPrimary = true
2323
""")
2424
void clearPrimaryFlagsByUser(User user);
25+
26+
boolean existsByUserAndNameAndDeletedAtIsNull(User user, String name);
2527
}

src/main/java/com/blockguard/server/global/common/codes/ErrorCode.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public enum ErrorCode {
2222
INVALID_TOKEN(HttpStatus.UNAUTHORIZED, 4020, "유효하지 않은 토큰입니다."),
2323
FILE_NAME_NOT_FOUND(HttpStatus.NOT_FOUND, 4021, "파일명이 없습니다."),
2424
INVALID_DIRECTORY_ROUTE(HttpStatus.NOT_FOUND, 4022, "잘못된 디렉토리 경로입니다."),
25+
FILE_SIZE_EXCEEDED(HttpStatus.PAYLOAD_TOO_LARGE, 4023, "프로필 파일 최대 허용 용량(5MB)을 초과했습니다."),
26+
DUPLICATE_GUARDIAN_NAME(HttpStatus.BAD_REQUEST, 4024, "이미 등록된 보호자 이름입니다."),
2527

2628
// 5000~ : server error
2729
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 5000, "서버 오류가 발생했습니다.");

src/main/java/com/blockguard/server/global/config/swagger/SwaggerResponseDescription.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public enum SwaggerResponseDescription {
2222
))),
2323

2424
UPDATE_MY_PAGE_INFO_FAIL(new LinkedHashSet<>(Set.of(
25-
ErrorCode.INVALID_DATE_FORMAT
25+
ErrorCode.INVALID_PHONE_NUMBER_FORMAT,
26+
ErrorCode.INVALID_PROFILE_IMAGE,
27+
ErrorCode.FILE_NAME_NOT_FOUND,
28+
ErrorCode.INVALID_DIRECTORY_ROUTE,
29+
ErrorCode.FILE_SIZE_EXCEEDED
2630
))),
2731

2832
FIND_PASSWORD_FAIL(new LinkedHashSet<>(Set.of(
@@ -37,15 +41,19 @@ public enum SwaggerResponseDescription {
3741
ErrorCode.INVALID_PHONE_NUMBER_FORMAT,
3842
ErrorCode.INVALID_PROFILE_IMAGE,
3943
ErrorCode.FILE_NAME_NOT_FOUND,
40-
ErrorCode.INVALID_DIRECTORY_ROUTE
44+
ErrorCode.INVALID_DIRECTORY_ROUTE,
45+
ErrorCode.FILE_SIZE_EXCEEDED,
46+
ErrorCode.DUPLICATE_GUARDIAN_NAME
4147
))),
4248

4349
UPDATE_GUARDIAN_FAIL(new LinkedHashSet<>(Set.of(
4450
ErrorCode.INVALID_PHONE_NUMBER_FORMAT,
4551
ErrorCode.INVALID_PROFILE_IMAGE,
4652
ErrorCode.GUARDIAN_NOT_FOUND,
4753
ErrorCode.FILE_NAME_NOT_FOUND,
48-
ErrorCode.INVALID_DIRECTORY_ROUTE
54+
ErrorCode.INVALID_DIRECTORY_ROUTE,
55+
ErrorCode.FILE_SIZE_EXCEEDED,
56+
ErrorCode.DUPLICATE_GUARDIAN_NAME
4957
))),
5058

5159
UPDATE_GUARDIAN_PRIMARY_FAIL(new LinkedHashSet<>(Set.of(

src/main/java/com/blockguard/server/global/exception/GlobalExceptionHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.http.ResponseEntity;
77
import org.springframework.web.bind.annotation.ExceptionHandler;
88
import org.springframework.web.bind.annotation.RestControllerAdvice;
9+
import org.springframework.web.multipart.MaxUploadSizeExceededException;
910

1011
@RestControllerAdvice
1112
@Slf4j
@@ -20,6 +21,14 @@ protected ResponseEntity<ErrorResponse> handleCustomException(BusinessExceptionH
2021
.body(ErrorResponse.of(errorCode));
2122
}
2223

24+
@ExceptionHandler(MaxUploadSizeExceededException.class)
25+
protected ResponseEntity<ErrorResponse> handleMaxUploadSize(MaxUploadSizeExceededException maxUploadSizeExceededException) {
26+
log.error("[MaxUploadSizeExceeded] message: {}", maxUploadSizeExceededException.getMessage());
27+
return ResponseEntity
28+
.status(ErrorCode.FILE_SIZE_EXCEEDED.getStatus())
29+
.body(ErrorResponse.of(ErrorCode.FILE_SIZE_EXCEEDED));
30+
}
31+
2332
@ExceptionHandler(Exception.class)
2433
protected ResponseEntity<ErrorResponse> handleException(Exception exception) {
2534
log.error("[Exception] message: {}", exception.getMessage(), exception);

0 commit comments

Comments
 (0)