Skip to content

Commit

Permalink
Merge pull request #162 from nhnacademy-be4-ckin/feature/exception
Browse files Browse the repository at this point in the history
[FEAT] Exception Handler
  • Loading branch information
f1v3-dev authored Mar 22, 2024
2 parents 670fe89 + b2ed615 commit d47a5c4
Show file tree
Hide file tree
Showing 48 changed files with 364 additions and 190 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package store.ckin.api.address.exception;

import store.ckin.api.advice.exception.GeneralAlreadyExistsException;

/**
* 이미 등록된 주소일 때 호출되는 Exception 클래스 입니다.
*
* @author : jinwoolee
* @version : 2024. 03. 18.
*/
public class AddressAlreadyExistsException extends RuntimeException{
public class AddressAlreadyExistsException extends GeneralAlreadyExistsException {

public AddressAlreadyExistsException(Long memberId, String base, String detail) {
super(String.format("이미 등록된 주소입니다. [회원 ID = %d, Address = %s %s] ", memberId, base, detail));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package store.ckin.api.address.exception;

import store.ckin.api.advice.exception.GeneralNotFoundException;

/**
* 조회한 주소가 없을 때 호출되는 Exception 클래스 입니다.
*
* @author : jinwoolee
* @version : 2024. 03. 18.
*/
public class AddressNotFoundException extends RuntimeException{
public class AddressNotFoundException extends GeneralNotFoundException {

public AddressNotFoundException(Long memberId, Long addressId) {
super(String.format("주소를 찾을 수 없습니다. [회원 ID = %d, 주소 ID = %d]", memberId, addressId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public class AddressServiceImpl implements AddressService {
public void addAddress(Long memberId,
AddressAddRequestDto addressAddRequestDto) {
Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
.orElseThrow(() -> new MemberNotFoundException(memberId));

if (addressRepository.existsByMemberIdAndBaseAndDetail(
memberId,
addressAddRequestDto.getBase(),
addressAddRequestDto.getDetail())) {
throw new AddressAlreadyExistsException();
throw new AddressAlreadyExistsException(memberId, addressAddRequestDto.getBase(),
addressAddRequestDto.getDetail());
}

Address address = Address.builder()
Expand All @@ -58,7 +59,7 @@ public void addAddress(Long memberId,
@Override
public List<MemberAddressResponseDto> getMemberAddressList(Long memberId) {
if (!memberRepository.existsById(memberId)) {
throw new MemberNotFoundException();
throw new MemberNotFoundException(memberId);
}

return addressRepository.getMemberAddressList(memberId);
Expand All @@ -69,12 +70,13 @@ public List<MemberAddressResponseDto> getMemberAddressList(Long memberId) {
public void updateAddress(Long memberId,
Long addressId,
AddressUpdateRequestDto addressUpdateRequestDto) {

if (!memberRepository.existsById(memberId)) {
throw new MemberNotFoundException();
throw new MemberNotFoundException(memberId);
}

Address address = addressRepository.findByIdAndMember_Id(addressId, memberId)
.orElseThrow(AddressNotFoundException::new);
.orElseThrow(() -> new AddressNotFoundException(memberId, addressId));

address.update(addressUpdateRequestDto);
}
Expand All @@ -83,15 +85,15 @@ public void updateAddress(Long memberId,
@Override
public void setDefaultAddress(Long memberId, Long addressId) {
if (!memberRepository.existsById(memberId)) {
throw new MemberNotFoundException();
throw new MemberNotFoundException(memberId);
}

Address address = addressRepository.findByIdAndMember_Id(addressId, memberId)
.orElseThrow(AddressNotFoundException::new);
.orElseThrow(() -> new AddressNotFoundException(memberId, addressId));

if (!address.getIsDefault()) {
addressRepository.findDefaultAddressByMemberId(memberId)
.ifPresent(Address::toggleDefault);
.ifPresent(Address::toggleDefault);

address.toggleDefault();
}
Expand All @@ -101,11 +103,11 @@ public void setDefaultAddress(Long memberId, Long addressId) {
@Override
public void deleteAddress(Long memberId, Long addressId) {
if (!memberRepository.existsById(memberId)) {
throw new MemberNotFoundException();
throw new MemberNotFoundException(memberId);
}

if (!addressRepository.existsByIdAndMember_Id(addressId, memberId)) {
throw new AddressNotFoundException();
throw new AddressNotFoundException(memberId, addressId);
}

addressRepository.deleteById(addressId);
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/store/ckin/api/advice/WebControllerAdvice.java

This file was deleted.

108 changes: 108 additions & 0 deletions src/main/java/store/ckin/api/advice/WebRestControllerAdvice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package store.ckin.api.advice;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import store.ckin.api.advice.exception.GeneralAlreadyExistsException;
import store.ckin.api.advice.exception.GeneralBadRequestException;
import store.ckin.api.advice.exception.GeneralForbiddenException;
import store.ckin.api.advice.exception.GeneralNotFoundException;
import store.ckin.api.common.dto.ErrorResponse;

/**
* Controller Advice 입니다.
*
* @author 정승조
* @version 2024. 02. 16.
*/

@RestControllerAdvice
public class WebRestControllerAdvice {

/**
* Validation 예외 처리 핸들러입니다.
*
* @param e MethodArgumentNotValidException
* @return 400(BAD REQUEST), Error Message
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleValidationException(MethodArgumentNotValidException e) {
List<String> errors = e.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.toList());

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errors);
}

/**
* 전역으로 발생하는 Not Found Exception 처리 핸들러입니다.
*
* @param e xxxNotFoundException
* @return 404(NOT FOUND), Error Message
*/
@ExceptionHandler(GeneralNotFoundException.class)
public ResponseEntity<ErrorResponse> handleNotFoundException(GeneralNotFoundException e) {

ErrorResponse errorResponse = ErrorResponse.builder()
.code(HttpStatus.NOT_FOUND)
.message(e.getMessage())
.build();

return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

/**
* 전역으로 발생하는 Already Exists Exception 처리 핸들러입니다.
*
* @param e xxxAlreadyExistsException
* @return 409(CONFLICT), Error Message
*/
@ExceptionHandler(GeneralAlreadyExistsException.class)
public ResponseEntity<ErrorResponse> handleAlreadyExistsException(GeneralAlreadyExistsException e) {

ErrorResponse errorResponse = ErrorResponse.builder()
.code(HttpStatus.CONFLICT)
.message(e.getMessage())
.build();

return ResponseEntity.status(HttpStatus.CONFLICT).body(errorResponse);
}

/**
* 전역으로 발생하는 BadRequest Exception 처리 핸들러입니다.
*
* @param e GeneralBadRequestException
* @return 400(BAD REQUEST), Error Message
*/
@ExceptionHandler(GeneralBadRequestException.class)
public ResponseEntity<ErrorResponse> handleBadRequestException(GeneralBadRequestException e) {
ErrorResponse errorResponse = ErrorResponse.builder()
.code(HttpStatus.BAD_REQUEST)
.message(e.getMessage())
.build();

return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
}

/**
* 전역으로 발생하는 Forbidden Exception 처리 핸들러입니다.
*
* @param e GeneralForbiddenException
* @return 403(FORBIDDEN), Error Message
*/
@ExceptionHandler(GeneralForbiddenException.class)
public ResponseEntity<ErrorResponse> handleForbiddenException(GeneralForbiddenException e) {
ErrorResponse errorResponse = ErrorResponse.builder()
.code(HttpStatus.FORBIDDEN)
.message(e.getMessage())
.build();

return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.ckin.api.advice.exception;

/**
* 이미 존재하는 데이터일 때 발생하는 에러를 전역으로 처리하기 위한 클래스.
*
* @author 정승조
* @version 2024. 03. 22.
*/
public class GeneralAlreadyExistsException extends RuntimeException {

public GeneralAlreadyExistsException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.ckin.api.advice.exception;

/**
* 잘못된 요청이 발생했을 때 발생하는 예외를 전역으로 처리하기 위한 클래스.
*
* @author 정승조
* @version 2024. 03. 22.
*/
public class GeneralBadRequestException extends RuntimeException {

public GeneralBadRequestException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.ckin.api.advice.exception;

/**
* 권한이 없을 때 발생하는 에러를 전역으로 처리하기 위한 클래스.
*
* @author 정승조
* @version 2024. 03. 22.
*/
public class GeneralForbiddenException extends RuntimeException {

public GeneralForbiddenException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package store.ckin.api.advice.exception;

/**
* 존재하지 않는 데이터일 때 발생하는 에러를 전역으로 처리하기 위한 클래스.
*
* @author 정승조
* @version 2024. 03. 22.
*/
public class GeneralNotFoundException extends RuntimeException {

public GeneralNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package store.ckin.api.author.exception;

import store.ckin.api.advice.exception.GeneralNotFoundException;

/**
* AuthorNotFoundException 클래스.
*
* @author 나국로
* @version 2024. 02. 16.
*/
public class AuthorNotFoundException extends RuntimeException {
public class AuthorNotFoundException extends GeneralNotFoundException {


/**
Expand All @@ -15,7 +17,7 @@ public class AuthorNotFoundException extends RuntimeException {
* @param authorId the author id
*/
public AuthorNotFoundException(Long authorId) {
super(String.format("Author not found: %s", authorId));
super(String.format("작가를 찾을 수 없습니다 [ID = %d]", authorId));
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package store.ckin.api.book.exception;

import store.ckin.api.advice.exception.GeneralNotFoundException;

/**
* BookNotFoundException 예외 클래스.
*
* @author 나국로
* @version 2024. 02. 26.
*/
public class BookNotFoundException extends RuntimeException {
public BookNotFoundException() {
}
public class BookNotFoundException extends GeneralNotFoundException {

public BookNotFoundException(Long bookId) {
super(String.format("책(id: %d)을 찾을 수 없습니다", bookId));
super(String.format("책을 찾을 수 없습니다 [id = %d]", bookId));

}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package store.ckin.api.category.exception;

import store.ckin.api.advice.exception.GeneralNotFoundException;

/**
* CategoryNotFoundException.
*
* @author 나국로
* @version 2024. 02. 16.
*/
public class CategoryNotFoundException extends RuntimeException {
public class CategoryNotFoundException extends GeneralNotFoundException {

public CategoryNotFoundException(Long categoryId) {
super(String.format("Category not found: %s", categoryId));
super(String.format("카테고리를 찾을 수 없습니다. [id = %d]", categoryId));
}
}
Loading

0 comments on commit d47a5c4

Please sign in to comment.