From a0315378234dcb110012871cfcc243ddf1ce9143 Mon Sep 17 00:00:00 2001 From: gkfktkrh153 Date: Mon, 18 Dec 2023 17:18:51 +0900 Subject: [PATCH] =?UTF-8?q?Feature=20:=20=EC=98=88=EC=99=B8=20=EC=A0=84?= =?UTF-8?q?=EC=97=AD=20=EC=B2=98=EB=A6=AC=20=EA=B5=AC=EC=A1=B0=20=EC=84=B8?= =?UTF-8?q?=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/CustomException.java | 14 +++++++ .../api/trip/common/exception/ErrorCode.java | 20 +++++++++ .../trip/common/exception/ErrorResponse.java | 12 ++++++ .../exception/GlobalExceptionHandler.java | 42 +++++++++++++++++++ .../custom_exception/NotFoundException.java | 10 +++++ 5 files changed, 98 insertions(+) create mode 100644 src/main/java/com/api/trip/common/exception/CustomException.java create mode 100644 src/main/java/com/api/trip/common/exception/ErrorCode.java create mode 100644 src/main/java/com/api/trip/common/exception/ErrorResponse.java create mode 100644 src/main/java/com/api/trip/common/exception/GlobalExceptionHandler.java create mode 100644 src/main/java/com/api/trip/common/exception/custom_exception/NotFoundException.java diff --git a/src/main/java/com/api/trip/common/exception/CustomException.java b/src/main/java/com/api/trip/common/exception/CustomException.java new file mode 100644 index 0000000..9887735 --- /dev/null +++ b/src/main/java/com/api/trip/common/exception/CustomException.java @@ -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; + + +} diff --git a/src/main/java/com/api/trip/common/exception/ErrorCode.java b/src/main/java/com/api/trip/common/exception/ErrorCode.java new file mode 100644 index 0000000..9cd543e --- /dev/null +++ b/src/main/java/com/api/trip/common/exception/ErrorCode.java @@ -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; + } + + +} diff --git a/src/main/java/com/api/trip/common/exception/ErrorResponse.java b/src/main/java/com/api/trip/common/exception/ErrorResponse.java new file mode 100644 index 0000000..6251604 --- /dev/null +++ b/src/main/java/com/api/trip/common/exception/ErrorResponse.java @@ -0,0 +1,12 @@ +package com.api.trip.common.exception; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class ErrorResponse{ + private String errorMessage; + + +} diff --git a/src/main/java/com/api/trip/common/exception/GlobalExceptionHandler.java b/src/main/java/com/api/trip/common/exception/GlobalExceptionHandler.java new file mode 100644 index 0000000..b108beb --- /dev/null +++ b/src/main/java/com/api/trip/common/exception/GlobalExceptionHandler.java @@ -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 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> handleValidationExceptions(MethodArgumentNotValidException ex){ + Map errors = new HashMap<>(); + ex.getBindingResult().getAllErrors() + .forEach(c -> errors.put(((FieldError) c).getField(), c.getDefaultMessage())); + log.error(ex.getMessage()); + return ResponseEntity.badRequest().body(errors); + } + +} diff --git a/src/main/java/com/api/trip/common/exception/custom_exception/NotFoundException.java b/src/main/java/com/api/trip/common/exception/custom_exception/NotFoundException.java new file mode 100644 index 0000000..80f8749 --- /dev/null +++ b/src/main/java/com/api/trip/common/exception/custom_exception/NotFoundException.java @@ -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); + } +}