-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #162 from nhnacademy-be4-ckin/feature/exception
[FEAT] Exception Handler
- Loading branch information
Showing
48 changed files
with
364 additions
and
190 deletions.
There are no files selected for viewing
8 changes: 7 additions & 1 deletion
8
src/main/java/store/ckin/api/address/exception/AddressAlreadyExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
8 changes: 7 additions & 1 deletion
8
src/main/java/store/ckin/api/address/exception/AddressNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 0 additions & 36 deletions
36
src/main/java/store/ckin/api/advice/WebControllerAdvice.java
This file was deleted.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
src/main/java/store/ckin/api/advice/WebRestControllerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/store/ckin/api/advice/exception/GeneralAlreadyExistsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/store/ckin/api/advice/exception/GeneralBadRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/store/ckin/api/advice/exception/GeneralForbiddenException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/store/ckin/api/advice/exception/GeneralNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
src/main/java/store/ckin/api/book/exception/BookNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
src/main/java/store/ckin/api/category/exception/CategoryNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
Oops, something went wrong.