Skip to content

Commit

Permalink
[REFACTOR] Exception 처리 구조 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
yeseul106 committed Aug 7, 2023
1 parent 40c80df commit ecc8e65
Show file tree
Hide file tree
Showing 23 changed files with 190 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import site.katchup.katchupserver.api.auth.dto.GoogleInfoDto;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.UnauthorizedException;
import site.katchup.katchupserver.common.response.ErrorStatus;

@RequiredArgsConstructor
Expand Down Expand Up @@ -48,7 +48,7 @@ public static GoogleInfoDto getGoogleData(String googleToken) {

return new GoogleInfoDto(nickname, email, imageUrl);
} catch (HttpClientErrorException e) {
throw new CustomException(ErrorStatus.GOOGLE_UNAUTHORIZED_USER);
throw new UnauthorizedException(ErrorStatus.GOOGLE_UNAUTHORIZED_USER);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import site.katchup.katchupserver.api.auth.service.GoogleAuthService;
import site.katchup.katchupserver.api.member.domain.Member;
import site.katchup.katchupserver.api.member.repository.MemberRepository;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.UnauthorizedException;
import site.katchup.katchupserver.common.response.ErrorStatus;
import site.katchup.katchupserver.config.jwt.JwtTokenProvider;
import site.katchup.katchupserver.config.jwt.UserAuthentication;
Expand Down Expand Up @@ -77,7 +77,7 @@ public AuthTokenGetResponseDto getNewToken(String accessToken, String refreshTok

private Member findMemberByEmail(String email) {
return memberRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorStatus.INVALID_MEMBER));
.orElseThrow(() -> new UnauthorizedException(ErrorStatus.INVALID_MEMBER));
}

private boolean isMemberByEmail(String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import site.katchup.katchupserver.api.task.repository.TaskRepository;
import site.katchup.katchupserver.api.trash.domain.Trash;
import site.katchup.katchupserver.api.trash.repository.TrashRepository;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.EntityNotFoundException;
import site.katchup.katchupserver.common.exception.BadRequestException;
import site.katchup.katchupserver.common.exception.NotFoundException;
import site.katchup.katchupserver.common.response.ErrorStatus;
import site.katchup.katchupserver.common.util.S3Util;

Expand Down Expand Up @@ -122,7 +122,7 @@ public void createCard(List<MultipartFile> fileList, CardCreateRequestDto reques
.build());
}
} catch (IOException e) {
throw new CustomException(ErrorStatus.FILE_UPLOAD_ERROR);
throw new BadRequestException(ErrorStatus.FILE_UPLOAD_ERROR);
}
}

Expand Down Expand Up @@ -174,7 +174,7 @@ private ObjectMetadata getObjectMetadata(MultipartFile file) {

private void validateFileSize(MultipartFile file) {
if (file.getSize() >= MAX_FILE_SIZE) {
throw new CustomException(ErrorStatus.FILE_SIZE_EXCEED);
throw new BadRequestException(ErrorStatus.FILE_SIZE_EXCEED);
}
}

Expand All @@ -198,34 +198,34 @@ private List<FileGetResponseDto> getFileDtoList(Long cardId) {

private Card getCardById(Long cardId) {
Card card = cardRepository.findById(cardId).orElseThrow(
() -> new EntityNotFoundException(ErrorStatus.NOT_FOUND_CARD)
() -> new NotFoundException(ErrorStatus.NOT_FOUND_CARD)
);

if (card.isDeleted()) {
throw new CustomException(ErrorStatus.DELETED_CARD);
throw new NotFoundException(ErrorStatus.DELETED_CARD);
}
return card;
}

private Folder getFolderById(Long folderId) {
return folderRepository.findById(folderId).orElseThrow(
() -> new EntityNotFoundException(ErrorStatus.NOT_FOUND_FOLDER));
() -> new NotFoundException(ErrorStatus.NOT_FOUND_FOLDER));
}

private Category getCategoryById(Long categoryId) {
return categoryRepository.findById(categoryId).orElseThrow(
() -> new EntityNotFoundException(ErrorStatus.NOT_FOUND_CATEGORY));
() -> new NotFoundException(ErrorStatus.NOT_FOUND_CATEGORY));
}

private void validatePdf(MultipartFile file) {
if (!file.getContentType().equals(PDF_TYPE)) {
throw new CustomException(ErrorStatus.NOT_PDF_FILE_TYPE);
throw new BadRequestException(ErrorStatus.NOT_PDF_FILE_TYPE);
}
}

private Task getTaskById(Long taskId) {
return taskRepository.findById(taskId).orElseThrow(
() -> new EntityNotFoundException(ErrorStatus.INVALID_TASK));
() -> new NotFoundException(ErrorStatus.INVALID_TASK));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import site.katchup.katchupserver.api.member.repository.MemberRepository;
import site.katchup.katchupserver.api.task.domain.Task;
import site.katchup.katchupserver.api.card.domain.Card;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.BadRequestException;
import site.katchup.katchupserver.common.exception.UnauthorizedException;
import site.katchup.katchupserver.common.response.ErrorStatus;

import java.util.List;
Expand All @@ -36,7 +37,7 @@ public class CategoryServiceImpl implements CategoryService {
@Transactional
public void createCategoryName(Long memberId, CategoryCreateRequestDto requestDto) {
if (checkDuplicateCategoryName(memberId, requestDto.getName())) {
throw new CustomException(ErrorStatus.DUPLICATE_CATEGORY_NAME);
throw new BadRequestException(ErrorStatus.DUPLICATE_CATEGORY_NAME);
}

categoryRepository.save(Category.builder()
Expand All @@ -61,7 +62,7 @@ public void updateCategoryName(Long memberId, Long categoryId, CategoryUpdateReq
.orElseThrow(() -> new EntityNotFoundException(NOT_FOUND_CATEGORY.getMessage()));

if (checkDuplicateCategoryName(memberId, requestDto.getName())) {
throw new CustomException(ErrorStatus.DUPLICATE_FOLDER_NAME);
throw new BadRequestException(ErrorStatus.DUPLICATE_FOLDER_NAME);
}

findCategory.updateCategoryName(requestDto.getName());
Expand Down Expand Up @@ -94,6 +95,6 @@ private void deleteFolderAndTaskAndCard(Folder folder) {

private Member findMember(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorStatus.INVALID_MEMBER));
.orElseThrow(() -> new UnauthorizedException(ErrorStatus.INVALID_MEMBER));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package site.katchup.katchupserver.api.common;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import site.katchup.katchupserver.api.card.domain.Card;
import site.katchup.katchupserver.api.card.repository.CardRepository;
import site.katchup.katchupserver.common.exception.NotFoundException;
import site.katchup.katchupserver.common.response.ErrorStatus;

@Service
Expand All @@ -14,6 +14,6 @@ public class CardProvider {
private final CardRepository cardRepository;

public Card getCardById(Long cardId) {
return cardRepository.findById(cardId).orElseThrow(() -> new EntityNotFoundException(ErrorStatus.NOT_FOUND_CARD.getMessage()));
return cardRepository.findById(cardId).orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_CARD));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import site.katchup.katchupserver.api.folder.repository.FolderRepository;
import site.katchup.katchupserver.api.folder.service.FolderService;
import site.katchup.katchupserver.api.task.domain.Task;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.EntityNotFoundException;
import site.katchup.katchupserver.common.exception.BadRequestException;
import site.katchup.katchupserver.common.exception.NotFoundException;
import site.katchup.katchupserver.common.response.ErrorStatus;

import java.util.List;
Expand Down Expand Up @@ -51,10 +51,10 @@ public List<FolderGetResponseDto> getByCategoryId(Long categoryId) {
@Transactional
public void updateFolderName(Long folderId, FolderUpdateRequestDto requestDto) {
Folder findFolder = folderRepository.findById(folderId)
.orElseThrow(() -> new EntityNotFoundException(NOT_FOUND_FOLDER));
.orElseThrow(() -> new NotFoundException(NOT_FOUND_FOLDER));

if (checkDuplicateFolderName(findFolder.getCategory().getId(), requestDto.getName())) {
throw new CustomException(ErrorStatus.DUPLICATE_FOLDER_NAME);
throw new BadRequestException(ErrorStatus.DUPLICATE_FOLDER_NAME);
}

findFolder.updateFolderName(requestDto.getName());
Expand All @@ -64,9 +64,9 @@ public void updateFolderName(Long folderId, FolderUpdateRequestDto requestDto) {
@Transactional
public void createFolderName(FolderCreateRequestDto requestDto) {
Category category = categoryRepository.findById(requestDto.getCategoryId())
.orElseThrow(() -> new EntityNotFoundException(NOT_FOUND_CATEGORY));
.orElseThrow(() -> new NotFoundException(NOT_FOUND_CATEGORY));
if (checkDuplicateFolderName(requestDto.getCategoryId(), requestDto.getName())) {
throw new CustomException(ErrorStatus.DUPLICATE_FOLDER_NAME);
throw new BadRequestException(ErrorStatus.DUPLICATE_FOLDER_NAME);
}

folderRepository.save(Folder.builder()
Expand Down Expand Up @@ -94,6 +94,6 @@ private void deleteTaskAndCard(Task task) {

private Folder getFolderById(Long folderId) {
return folderRepository.findById(folderId)
.orElseThrow(() -> new EntityNotFoundException(NOT_FOUND_FOLDER));
.orElseThrow(() -> new NotFoundException(NOT_FOUND_FOLDER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import site.katchup.katchupserver.api.member.dto.MemberProfileGetResponseDto;
import site.katchup.katchupserver.api.member.repository.MemberRepository;
import site.katchup.katchupserver.api.member.service.MemberService;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.UnauthorizedException;
import site.katchup.katchupserver.common.response.ErrorStatus;

@Service
Expand All @@ -25,6 +25,6 @@ public MemberProfileGetResponseDto getMemberProfile(Long memberId) {

private Member findMember(Long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorStatus.INVALID_MEMBER));
.orElseThrow(() -> new UnauthorizedException(ErrorStatus.INVALID_MEMBER));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import site.katchup.katchupserver.api.screenshot.repository.ScreenshotRepository;
import site.katchup.katchupserver.api.screenshot.service.ScreenshotService;
import site.katchup.katchupserver.api.screenshot.service.ScreenshotValidator;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.InternalServerException;
import site.katchup.katchupserver.common.response.ErrorStatus;
import site.katchup.katchupserver.common.util.S3Util;

Expand Down Expand Up @@ -63,7 +63,7 @@ public ScreenshotUploadResponseDto uploadScreenshot(MultipartFile file, Long car
.build();

} catch (Exception e) {
throw new CustomException(ErrorStatus.IMAGE_UPLOAD_EXCEPTION);
throw new InternalServerException(ErrorStatus.IMAGE_UPLOAD_EXCEPTION);
}
}

Expand Down Expand Up @@ -99,7 +99,7 @@ private String extractExtension(MultipartFile file) {
try {
return file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
} catch (StringIndexOutOfBoundsException e) {
throw new CustomException(ErrorStatus.IMAGE_UPLOAD_EXCEPTION);
throw new InternalServerException(ErrorStatus.IMAGE_UPLOAD_EXCEPTION);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import site.katchup.katchupserver.api.task.dto.response.TaskGetResponseDto;
import site.katchup.katchupserver.api.task.repository.TaskRepository;
import site.katchup.katchupserver.api.task.service.TaskService;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.NotFoundException;
import site.katchup.katchupserver.common.response.ErrorStatus;

import java.util.List;
Expand All @@ -37,7 +37,7 @@ public List<TaskGetResponseDto> getAllTask(Long memberId) {
@Override
public void createTask(TaskCreateRequestDto requestDto) {
Folder folder = folderRepository.findById(requestDto.getFolderId())
.orElseThrow(() -> new CustomException(ErrorStatus.NOT_FOUND_FOLDER));
.orElseThrow(() -> new NotFoundException(ErrorStatus.NOT_FOUND_FOLDER));

Task task = new Task(requestDto.getName(), folder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,37 @@

import jakarta.persistence.EntityNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import site.katchup.katchupserver.common.dto.ApiResponseDto;
import site.katchup.katchupserver.common.exception.CustomException;
import site.katchup.katchupserver.common.exception.BaseException;
import site.katchup.katchupserver.common.response.ErrorStatus;

import java.util.List;
import java.util.stream.Collectors;

@RestControllerAdvice
public class ControllerExceptionAdvice {

/**
* CustomException
*/
@ExceptionHandler(CustomException.class)
protected ApiResponseDto handleCustomException(final CustomException e) {
return ApiResponseDto.error(e.getErrorStatus());
@ExceptionHandler(BaseException.class)
public ResponseEntity<ApiResponseDto> handleGlobalException(BaseException ex) {
return ResponseEntity.status(ex.getStatusCode())
.body(ApiResponseDto.error(ex.getStatusCode().value(), ex.getResponseMessage()));
}

/**
* EntityNotFoundException
*/
@ExceptionHandler(EntityNotFoundException.class)
protected ApiResponseDto handleEntityNotFoundException(final EntityNotFoundException e) {
return ApiResponseDto.error(ErrorStatus.valueOf(e.getMessage()));
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<ApiResponseDto> handleMissingParameter(MissingServletRequestParameterException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponseDto.error(HttpStatus.BAD_REQUEST.value(),
ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION.getMessage()));
}

/**
* 400 BAD_REQUEST
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ApiResponseDto handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
String errorMessage = e.getFieldError().getDefaultMessage();
if (errorMessage.equals(ErrorStatus.VALIDATION_NAMING_EXCEPTION.getMessage()))
return ApiResponseDto.error(ErrorStatus.VALIDATION_NAMING_EXCEPTION);
return ApiResponseDto.error(ErrorStatus.VALIDATION_EXCEPTION);
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
protected ApiResponseDto handleIllegalArgumentException(IllegalArgumentException e ) {
return ApiResponseDto.error(ErrorStatus.valueOf(e.getMessage()));
public ResponseEntity<ApiResponseDto> handleIllegalArgument(IllegalArgumentException ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(ApiResponseDto.error(HttpStatus.BAD_REQUEST.value(), ex.getMessage()));
}

/**
* 500 INTERNAL_SERVER_ERROR
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public ApiResponseDto unhandledExceptionHandler(final Exception e) {
return ApiResponseDto.error(ErrorStatus.INTERNAL_SERVER_ERROR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ public class ApiResponseDto<T> {
private final String message;
private T data;

public static ApiResponseDto success(SuccessStatus status, Object data) {
return ApiResponseDto.builder()
.status(status.getHttpStatus().value())
.success(true)
.message(status.getMessage())
.data(data)
.build();
}

public static ApiResponseDto success(SuccessStatus successStatus) {
return ApiResponseDto.builder()
.status(successStatus.getHttpStatus().value())
Expand All @@ -22,12 +31,12 @@ public static ApiResponseDto success(SuccessStatus successStatus) {
.build();
}

public static <T> ApiResponseDto<T> success(SuccessStatus successStatus, T data) {
return new ApiResponseDto<T>(successStatus.getHttpStatus().value(), true, successStatus.getMessage(), data);
}

public static ApiResponseDto error(ErrorStatus errorStatus) {
return new ApiResponseDto<>(errorStatus.getHttpStatus().value(), false, errorStatus.getMessage());
public static ApiResponseDto error(int status, String message) {
return ApiResponseDto.builder()
.status(status)
.success(false)
.message(message)
.build();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package site.katchup.katchupserver.common.exception;

import org.springframework.http.HttpStatus;
import site.katchup.katchupserver.common.response.ErrorStatus;

public class BadRequestException extends BaseException {

public BadRequestException(ErrorStatus errorStatus) {
super(HttpStatus.BAD_REQUEST, errorStatus.getMessage());
}

public BadRequestException(String message) {
super(HttpStatus.BAD_REQUEST, message);
}

}
Loading

0 comments on commit ecc8e65

Please sign in to comment.