-
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.
Browse files
Browse the repository at this point in the history
예외 전역 처리 구조 세팅
- Loading branch information
Showing
5 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/com/api/trip/common/exception/CustomException.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 com.api.trip.common.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public abstract class CustomException extends RuntimeException { | ||
|
||
protected CustomException(ErrorCode errorCode){ | ||
this.errorCode = errorCode; | ||
} | ||
private ErrorCode errorCode; | ||
|
||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/api/trip/common/exception/ErrorCode.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,20 @@ | ||
package com.api.trip.common.exception; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum ErrorCode { | ||
|
||
NOT_FOUND_MEMBER("회원이 존재하지 않습니다.", HttpStatus.NOT_FOUND); | ||
|
||
private final String message; | ||
private final HttpStatus status; | ||
|
||
ErrorCode(String message, HttpStatus status) { | ||
this.message = message; | ||
this.status = status; | ||
} | ||
|
||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/api/trip/common/exception/ErrorResponse.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,12 @@ | ||
package com.api.trip.common.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
public class ErrorResponse{ | ||
private String errorMessage; | ||
|
||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/api/trip/common/exception/GlobalExceptionHandler.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,42 @@ | ||
package com.api.trip.common.exception; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.multipart.MaxUploadSizeExceededException; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
/** | ||
* 서비스 로직 도중 발생하는 에러들을 커스텀하여 응답값을 내려줍니다. | ||
*/ | ||
@ExceptionHandler(CustomException.class) | ||
public ResponseEntity<ErrorResponse> handleCustomException(CustomException e){ | ||
ErrorCode errorCode = e.getErrorCode(); | ||
log.error(errorCode.getMessage()); | ||
return ResponseEntity.status(errorCode.getStatus()).body( new ErrorResponse(errorCode.getMessage())); | ||
} | ||
|
||
/** | ||
* 바인딩 에러 json형식으로 에러가 있는 파라미터 값에 메시지를 담아 넘겨줍니다. | ||
* itemPrice : 아이템 가격을 100원 이상 1,000,000,000원 이하여야 합니다. | ||
*/ | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
public ResponseEntity<Map<String, String>> handleValidationExceptions(MethodArgumentNotValidException ex){ | ||
Map<String, String> errors = new HashMap<>(); | ||
ex.getBindingResult().getAllErrors() | ||
.forEach(c -> errors.put(((FieldError) c).getField(), c.getDefaultMessage())); | ||
log.error(ex.getMessage()); | ||
return ResponseEntity.badRequest().body(errors); | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/api/trip/common/exception/custom_exception/NotFoundException.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,10 @@ | ||
package com.api.trip.common.exception.custom_exception; | ||
|
||
import com.api.trip.common.exception.CustomException; | ||
import com.api.trip.common.exception.ErrorCode; | ||
|
||
public class NotFoundException extends CustomException { | ||
public NotFoundException(ErrorCode errorCode) { | ||
super(errorCode); | ||
} | ||
} |