From 3fe32ca7c3753bd9a9131f06508418bee9e81f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Wed, 28 Jun 2023 20:28:27 +0900 Subject: [PATCH 01/12] feat: Create ApiException and related subclasses --- .../universe/uni/exception/ApiException.java | 20 +++++++++++++++++++ .../uni/exception/BadRequestException.java | 10 ++++++++++ .../uni/exception/ConflictException.java | 10 ++++++++++ .../uni/exception/NotFoundException.java | 10 ++++++++++ .../uni/exception/UnauthorizedException.java | 10 ++++++++++ 5 files changed, 60 insertions(+) create mode 100644 src/main/java/com/universe/uni/exception/ApiException.java create mode 100644 src/main/java/com/universe/uni/exception/BadRequestException.java create mode 100644 src/main/java/com/universe/uni/exception/ConflictException.java create mode 100644 src/main/java/com/universe/uni/exception/NotFoundException.java create mode 100644 src/main/java/com/universe/uni/exception/UnauthorizedException.java diff --git a/src/main/java/com/universe/uni/exception/ApiException.java b/src/main/java/com/universe/uni/exception/ApiException.java new file mode 100644 index 0000000..e06ba95 --- /dev/null +++ b/src/main/java/com/universe/uni/exception/ApiException.java @@ -0,0 +1,20 @@ +package com.universe.uni.exception; + +import com.universe.uni.common.dto.ErrorType; + +public class ApiException extends RuntimeException { + private final ErrorType error; + + public ApiException(ErrorType error, String message) { + super(message); + this.error = error; + } + + public int getHttpStatus() { + return this.error.getHttpStatusCode(); + } + + public ErrorType getError() { + return this.error; + } +} diff --git a/src/main/java/com/universe/uni/exception/BadRequestException.java b/src/main/java/com/universe/uni/exception/BadRequestException.java new file mode 100644 index 0000000..631d648 --- /dev/null +++ b/src/main/java/com/universe/uni/exception/BadRequestException.java @@ -0,0 +1,10 @@ +package com.universe.uni.exception; + +import com.universe.uni.common.dto.ErrorType; + +public class BadRequestException extends ApiException { + + public BadRequestException(ErrorType error) { + super(error, error.getMessage()); + } +} diff --git a/src/main/java/com/universe/uni/exception/ConflictException.java b/src/main/java/com/universe/uni/exception/ConflictException.java new file mode 100644 index 0000000..c8b75cc --- /dev/null +++ b/src/main/java/com/universe/uni/exception/ConflictException.java @@ -0,0 +1,10 @@ +package com.universe.uni.exception; + +import com.universe.uni.common.dto.ErrorType; + +public class ConflictException extends ApiException { + + public ConflictException(ErrorType error) { + super(error, error.getMessage()); + } +} diff --git a/src/main/java/com/universe/uni/exception/NotFoundException.java b/src/main/java/com/universe/uni/exception/NotFoundException.java new file mode 100644 index 0000000..e6720d1 --- /dev/null +++ b/src/main/java/com/universe/uni/exception/NotFoundException.java @@ -0,0 +1,10 @@ +package com.universe.uni.exception; + +import com.universe.uni.common.dto.ErrorType; + +public class NotFoundException extends ApiException { + + public NotFoundException(ErrorType error) { + super(error, error.getMessage()); + } +} diff --git a/src/main/java/com/universe/uni/exception/UnauthorizedException.java b/src/main/java/com/universe/uni/exception/UnauthorizedException.java new file mode 100644 index 0000000..0e5eb9a --- /dev/null +++ b/src/main/java/com/universe/uni/exception/UnauthorizedException.java @@ -0,0 +1,10 @@ +package com.universe.uni.exception; + +import com.universe.uni.common.dto.ErrorType; + +public class UnauthorizedException extends ApiException { + + public UnauthorizedException(ErrorType error) { + super(error, error.getMessage()); + } +} From 6d471934f64dbc1105372c76c880fc878e885821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Wed, 28 Jun 2023 20:31:38 +0900 Subject: [PATCH 02/12] feat: Implement ErrorResponse --- .../uni/common/dto/ErrorResponse.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/com/universe/uni/common/dto/ErrorResponse.java diff --git a/src/main/java/com/universe/uni/common/dto/ErrorResponse.java b/src/main/java/com/universe/uni/common/dto/ErrorResponse.java new file mode 100644 index 0000000..4eeb217 --- /dev/null +++ b/src/main/java/com/universe/uni/common/dto/ErrorResponse.java @@ -0,0 +1,24 @@ +package com.universe.uni.common.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class ErrorResponse { + + private final int status; + private final String message; + + public static ErrorResponse error(ErrorType error) { + return new ErrorResponse(error.getHttpStatusCode(), error.getMessage()); + } + + public static ErrorResponse error(ErrorType error, String message) { + return new ErrorResponse(error.getHttpStatusCode(), message); + } +} From 4cccfc2abf4e1c99282e3e32e6afaa321fdc2e51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Wed, 28 Jun 2023 20:31:47 +0900 Subject: [PATCH 03/12] feat: Implement ErrorType --- .../universe/uni/common/dto/ErrorType.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 src/main/java/com/universe/uni/common/dto/ErrorType.java diff --git a/src/main/java/com/universe/uni/common/dto/ErrorType.java b/src/main/java/com/universe/uni/common/dto/ErrorType.java new file mode 100644 index 0000000..2e7a8a0 --- /dev/null +++ b/src/main/java/com/universe/uni/common/dto/ErrorType.java @@ -0,0 +1,42 @@ +package com.universe.uni.common.dto; + +import org.springframework.http.HttpStatus; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +public enum ErrorType { + + /** + * 400 BAD REQUEST + */ + VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), VALIDATION_REQUEST_MISSING_EXCEPTION( + HttpStatus.BAD_REQUEST, "요청값이 입력되지 않았습니다."), + + /** + * 404 NOT FOUND + */ + NOT_FOUND_USER_EXCEPTION(HttpStatus.NOT_FOUND, "존재하지 않는 유저입니다"), + + /** + * 409 CONFLICT + */ + ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 유저입니다"), + + /** + * 500 INTERNAL SERVER ERROR + */ + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "예상치 못한 서버 에러가 발생하였습니다."); + + private final HttpStatus httpStatus; + private final String message; + + public int getHttpStatusCode() { + return httpStatus.value(); + } + + public String getMessage() { + return message; + } +} From c79f69103b31b6f6881fac9d33735848a1d152d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Wed, 28 Jun 2023 20:31:58 +0900 Subject: [PATCH 04/12] feat: Implement ControllerExceptionAdvice --- .../uni/common/ControllerExceptionAdvice.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java diff --git a/src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java b/src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java new file mode 100644 index 0000000..4de0587 --- /dev/null +++ b/src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java @@ -0,0 +1,58 @@ +package com.universe.uni.common; + +import java.util.Objects; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.FieldError; +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 com.universe.uni.common.dto.ErrorResponse; +import com.universe.uni.common.dto.ErrorType; +import com.universe.uni.exception.ApiException; + +@RestControllerAdvice +public class ControllerExceptionAdvice { + + /** + * 400 BAD_REQUEST + */ + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MethodArgumentNotValidException.class) + protected ErrorResponse handleMethodArgumentNotValidException(final MethodArgumentNotValidException exception) { + FieldError fieldError = Objects.requireNonNull(exception.getBindingResult().getFieldError()); + String errorMessage = String.format("%s: %s", fieldError.getField(), fieldError.getDefaultMessage()); + return ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION, + String.format("%s", fieldError.getDefaultMessage(), fieldError.getField())); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(MissingServletRequestParameterException.class) + protected ErrorResponse handleMissingServletRequestParameterException( + MissingServletRequestParameterException exception) { + String errorMessage = String.format("'%s' 파라미터가 누락되었습니다.", exception.getParameterName()); + return ErrorResponse.error(ErrorType.VALIDATION_EXCEPTION, errorMessage); + } + + /** + * 500 Internal Server + */ + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + protected ErrorResponse handleException(final Exception exception) { + return ErrorResponse.error(ErrorType.INTERNAL_SERVER_ERROR); + } + + /** + * Api custom error + */ + @ExceptionHandler(ApiException.class) + protected ResponseEntity handleCustomException(ApiException exception) { + return ResponseEntity.status(exception.getHttpStatus()) + .body(ErrorResponse.error(exception.getError(), exception.getMessage())); + } +} From 80a73cdc86b1e927aac62246dacb50565fe0cf91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Wed, 28 Jun 2023 20:40:25 +0900 Subject: [PATCH 05/12] style: fix for formatting --- src/main/java/com/universe/uni/common/dto/ErrorType.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/universe/uni/common/dto/ErrorType.java b/src/main/java/com/universe/uni/common/dto/ErrorType.java index 2e7a8a0..a7eaded 100644 --- a/src/main/java/com/universe/uni/common/dto/ErrorType.java +++ b/src/main/java/com/universe/uni/common/dto/ErrorType.java @@ -11,8 +11,8 @@ public enum ErrorType { /** * 400 BAD REQUEST */ - VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), VALIDATION_REQUEST_MISSING_EXCEPTION( - HttpStatus.BAD_REQUEST, "요청값이 입력되지 않았습니다."), + VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), + VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "요청값이 입력되지 않았습니다."), /** * 404 NOT FOUND From daeb57743d9ee5f4f1c8b40302692e51268fd933 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Sun, 2 Jul 2023 00:59:51 +0900 Subject: [PATCH 06/12] chore: Move Error to a new package --- src/main/java/com/universe/uni/exception/ApiException.java | 2 +- .../com/universe/uni/exception/BadRequestException.java | 2 +- .../java/com/universe/uni/exception/ConflictException.java | 2 +- .../java/com/universe/uni/exception/NotFoundException.java | 2 +- .../com/universe/uni/exception/UnauthorizedException.java | 2 +- .../uni/{common => exception}/dto/ErrorResponse.java | 7 +++---- .../universe/uni/{common => exception}/dto/ErrorType.java | 2 +- 7 files changed, 9 insertions(+), 10 deletions(-) rename src/main/java/com/universe/uni/{common => exception}/dto/ErrorResponse.java (68%) rename src/main/java/com/universe/uni/{common => exception}/dto/ErrorType.java (96%) diff --git a/src/main/java/com/universe/uni/exception/ApiException.java b/src/main/java/com/universe/uni/exception/ApiException.java index e06ba95..9936812 100644 --- a/src/main/java/com/universe/uni/exception/ApiException.java +++ b/src/main/java/com/universe/uni/exception/ApiException.java @@ -1,6 +1,6 @@ package com.universe.uni.exception; -import com.universe.uni.common.dto.ErrorType; +import com.universe.uni.exception.dto.ErrorType; public class ApiException extends RuntimeException { private final ErrorType error; diff --git a/src/main/java/com/universe/uni/exception/BadRequestException.java b/src/main/java/com/universe/uni/exception/BadRequestException.java index 631d648..29e4881 100644 --- a/src/main/java/com/universe/uni/exception/BadRequestException.java +++ b/src/main/java/com/universe/uni/exception/BadRequestException.java @@ -1,6 +1,6 @@ package com.universe.uni.exception; -import com.universe.uni.common.dto.ErrorType; +import com.universe.uni.exception.dto.ErrorType; public class BadRequestException extends ApiException { diff --git a/src/main/java/com/universe/uni/exception/ConflictException.java b/src/main/java/com/universe/uni/exception/ConflictException.java index c8b75cc..f28b824 100644 --- a/src/main/java/com/universe/uni/exception/ConflictException.java +++ b/src/main/java/com/universe/uni/exception/ConflictException.java @@ -1,6 +1,6 @@ package com.universe.uni.exception; -import com.universe.uni.common.dto.ErrorType; +import com.universe.uni.exception.dto.ErrorType; public class ConflictException extends ApiException { diff --git a/src/main/java/com/universe/uni/exception/NotFoundException.java b/src/main/java/com/universe/uni/exception/NotFoundException.java index e6720d1..60d3033 100644 --- a/src/main/java/com/universe/uni/exception/NotFoundException.java +++ b/src/main/java/com/universe/uni/exception/NotFoundException.java @@ -1,6 +1,6 @@ package com.universe.uni.exception; -import com.universe.uni.common.dto.ErrorType; +import com.universe.uni.exception.dto.ErrorType; public class NotFoundException extends ApiException { diff --git a/src/main/java/com/universe/uni/exception/UnauthorizedException.java b/src/main/java/com/universe/uni/exception/UnauthorizedException.java index 0e5eb9a..f193ff2 100644 --- a/src/main/java/com/universe/uni/exception/UnauthorizedException.java +++ b/src/main/java/com/universe/uni/exception/UnauthorizedException.java @@ -1,6 +1,6 @@ package com.universe.uni.exception; -import com.universe.uni.common.dto.ErrorType; +import com.universe.uni.exception.dto.ErrorType; public class UnauthorizedException extends ApiException { diff --git a/src/main/java/com/universe/uni/common/dto/ErrorResponse.java b/src/main/java/com/universe/uni/exception/dto/ErrorResponse.java similarity index 68% rename from src/main/java/com/universe/uni/common/dto/ErrorResponse.java rename to src/main/java/com/universe/uni/exception/dto/ErrorResponse.java index 4eeb217..ed37693 100644 --- a/src/main/java/com/universe/uni/common/dto/ErrorResponse.java +++ b/src/main/java/com/universe/uni/exception/dto/ErrorResponse.java @@ -1,4 +1,4 @@ -package com.universe.uni.common.dto; +package com.universe.uni.exception.dto; import com.fasterxml.jackson.annotation.JsonInclude; @@ -11,14 +11,13 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class ErrorResponse { - private final int status; private final String message; public static ErrorResponse error(ErrorType error) { - return new ErrorResponse(error.getHttpStatusCode(), error.getMessage()); + return new ErrorResponse(error.getMessage()); } public static ErrorResponse error(ErrorType error, String message) { - return new ErrorResponse(error.getHttpStatusCode(), message); + return new ErrorResponse(message); } } diff --git a/src/main/java/com/universe/uni/common/dto/ErrorType.java b/src/main/java/com/universe/uni/exception/dto/ErrorType.java similarity index 96% rename from src/main/java/com/universe/uni/common/dto/ErrorType.java rename to src/main/java/com/universe/uni/exception/dto/ErrorType.java index a7eaded..bc0f421 100644 --- a/src/main/java/com/universe/uni/common/dto/ErrorType.java +++ b/src/main/java/com/universe/uni/exception/dto/ErrorType.java @@ -1,4 +1,4 @@ -package com.universe.uni.common.dto; +package com.universe.uni.exception.dto; import org.springframework.http.HttpStatus; From 3eb1ba9f2b9263857c670b18dc65c6e3fb9e1517 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Mon, 3 Jul 2023 17:38:55 +0900 Subject: [PATCH 07/12] fix: Change the message to unicode in Error Response --- .../universe/uni/exception/dto/ErrorResponse.java | 8 ++------ .../com/universe/uni/exception/dto/ErrorType.java | 15 ++++++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/universe/uni/exception/dto/ErrorResponse.java b/src/main/java/com/universe/uni/exception/dto/ErrorResponse.java index ed37693..6ce1870 100644 --- a/src/main/java/com/universe/uni/exception/dto/ErrorResponse.java +++ b/src/main/java/com/universe/uni/exception/dto/ErrorResponse.java @@ -11,13 +11,9 @@ @JsonInclude(JsonInclude.Include.NON_NULL) public class ErrorResponse { - private final String message; + private final String uniErrorCode; public static ErrorResponse error(ErrorType error) { - return new ErrorResponse(error.getMessage()); - } - - public static ErrorResponse error(ErrorType error, String message) { - return new ErrorResponse(message); + return new ErrorResponse(error.getUniErrorCode()); } } diff --git a/src/main/java/com/universe/uni/exception/dto/ErrorType.java b/src/main/java/com/universe/uni/exception/dto/ErrorType.java index bc0f421..d21483a 100644 --- a/src/main/java/com/universe/uni/exception/dto/ErrorType.java +++ b/src/main/java/com/universe/uni/exception/dto/ErrorType.java @@ -11,31 +11,36 @@ public enum ErrorType { /** * 400 BAD REQUEST */ - VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), - VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "요청값이 입력되지 않았습니다."), + VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "", "잘못된 요청입니다."), + VALIDATION_REQUEST_MISSING_EXCEPTION(HttpStatus.BAD_REQUEST, "", "요청값이 입력되지 않았습니다."), /** * 404 NOT FOUND */ - NOT_FOUND_USER_EXCEPTION(HttpStatus.NOT_FOUND, "존재하지 않는 유저입니다"), + NOT_FOUND_USER_EXCEPTION(HttpStatus.NOT_FOUND, "", "존재하지 않는 유저입니다"), /** * 409 CONFLICT */ - ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "이미 존재하는 유저입니다"), + ALREADY_EXIST_USER_EXCEPTION(HttpStatus.CONFLICT, "", "이미 존재하는 유저입니다"), /** * 500 INTERNAL SERVER ERROR */ - INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "예상치 못한 서버 에러가 발생하였습니다."); + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "", "예상치 못한 서버 에러가 발생하였습니다."); private final HttpStatus httpStatus; + private String uniErrorCode; private final String message; public int getHttpStatusCode() { return httpStatus.value(); } + public String getUniErrorCode() { + return uniErrorCode; + } + public String getMessage() { return message; } From 0880c1895106b798c6b178d3f0fbbee89c273292 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Mon, 3 Jul 2023 17:43:05 +0900 Subject: [PATCH 08/12] fix: Change by extending ResponseEntityExceptionHandler --- .../uni/common/ControllerExceptionAdvice.java | 58 -------------- .../controller/ControllerExceptionAdvice.java | 76 +++++++++++++++++++ 2 files changed, 76 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java create mode 100644 src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java diff --git a/src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java b/src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java deleted file mode 100644 index 4de0587..0000000 --- a/src/main/java/com/universe/uni/common/ControllerExceptionAdvice.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.universe.uni.common; - -import java.util.Objects; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.validation.FieldError; -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 com.universe.uni.common.dto.ErrorResponse; -import com.universe.uni.common.dto.ErrorType; -import com.universe.uni.exception.ApiException; - -@RestControllerAdvice -public class ControllerExceptionAdvice { - - /** - * 400 BAD_REQUEST - */ - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(MethodArgumentNotValidException.class) - protected ErrorResponse handleMethodArgumentNotValidException(final MethodArgumentNotValidException exception) { - FieldError fieldError = Objects.requireNonNull(exception.getBindingResult().getFieldError()); - String errorMessage = String.format("%s: %s", fieldError.getField(), fieldError.getDefaultMessage()); - return ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION, - String.format("%s", fieldError.getDefaultMessage(), fieldError.getField())); - } - - @ResponseStatus(HttpStatus.BAD_REQUEST) - @ExceptionHandler(MissingServletRequestParameterException.class) - protected ErrorResponse handleMissingServletRequestParameterException( - MissingServletRequestParameterException exception) { - String errorMessage = String.format("'%s' 파라미터가 누락되었습니다.", exception.getParameterName()); - return ErrorResponse.error(ErrorType.VALIDATION_EXCEPTION, errorMessage); - } - - /** - * 500 Internal Server - */ - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - @ExceptionHandler(Exception.class) - protected ErrorResponse handleException(final Exception exception) { - return ErrorResponse.error(ErrorType.INTERNAL_SERVER_ERROR); - } - - /** - * Api custom error - */ - @ExceptionHandler(ApiException.class) - protected ResponseEntity handleCustomException(ApiException exception) { - return ResponseEntity.status(exception.getHttpStatus()) - .body(ErrorResponse.error(exception.getError(), exception.getMessage())); - } -} diff --git a/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java b/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java new file mode 100644 index 0000000..7f86cd0 --- /dev/null +++ b/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java @@ -0,0 +1,76 @@ +package com.universe.uni.controller; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingPathVariableException; +import org.springframework.web.bind.MissingRequestHeaderException; +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 org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +import com.universe.uni.exception.ApiException; +import com.universe.uni.exception.dto.ErrorResponse; +import com.universe.uni.exception.dto.ErrorType; + +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@RestControllerAdvice +public class ControllerExceptionAdvice extends ResponseEntityExceptionHandler { + + /** + * 400 BAD_REQUEST + */ + @Override + protected ResponseEntity handleMethodArgumentNotValid( + MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { + ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } + + @Override + protected ResponseEntity handleMissingServletRequestParameter( + MissingServletRequestParameterException exception, + HttpHeaders headers, HttpStatus status, WebRequest request) { + ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } + + @Override + protected ResponseEntity handleMissingPathVariable(MissingPathVariableException exception, + HttpHeaders headers, + HttpStatus status, WebRequest request) { + ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } + + @ExceptionHandler(MissingRequestHeaderException.class) + protected ResponseEntity handleMissingRequestHeaderException( + MissingRequestHeaderException exception) { + ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_EXCEPTION); + return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); + } + + /** + * 500 Internal Server + */ + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + protected ErrorResponse handleException(final Exception exception) { + return ErrorResponse.error(ErrorType.INTERNAL_SERVER_ERROR); + } + + /** + * Api custom error + */ + @ExceptionHandler(ApiException.class) + protected ResponseEntity handleCustomException(ApiException exception) { + return ResponseEntity.status(exception.getHttpStatus()) + .body(ErrorResponse.error(exception.getError())); + } +} From dc8b2288f061941e5799b0579252ad05a1fd89eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Mon, 3 Jul 2023 17:45:20 +0900 Subject: [PATCH 09/12] feat: Add Interceptor and WebMvcConfig --- .../com/universe/uni/config/WebMvcConfig.java | 20 ++++++++++++++++ .../config/interceptor/AuthInterceptor.java | 23 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 src/main/java/com/universe/uni/config/WebMvcConfig.java create mode 100644 src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java diff --git a/src/main/java/com/universe/uni/config/WebMvcConfig.java b/src/main/java/com/universe/uni/config/WebMvcConfig.java new file mode 100644 index 0000000..bf88d4a --- /dev/null +++ b/src/main/java/com/universe/uni/config/WebMvcConfig.java @@ -0,0 +1,20 @@ +package com.universe.uni.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import com.universe.uni.config.interceptor.AuthInterceptor; + +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + + @Autowired + private AuthInterceptor authInterceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(authInterceptor); + } +} diff --git a/src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java b/src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java new file mode 100644 index 0000000..946c420 --- /dev/null +++ b/src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java @@ -0,0 +1,23 @@ +package com.universe.uni.config.interceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +@Component +public class AuthInterceptor implements HandlerInterceptor { + + /* TODO 토큰변수 추가 */ + + private static final String AUTHORIZATION = "Authorization"; + + @Override + public boolean preHandle( + HttpServletRequest request, HttpServletResponse response, Object handler + ) throws Exception { + /* TODO 토큰 header exception 처리 */ + return true; + } +} From 51e9933d5845d8f01b76f2bf449e5d71cc2f9ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Tue, 4 Jul 2023 05:00:20 +0900 Subject: [PATCH 10/12] chore: Delete Interceptor and WebMvcConfig --- .../com/universe/uni/config/WebMvcConfig.java | 20 ---------------- .../config/interceptor/AuthInterceptor.java | 23 ------------------- 2 files changed, 43 deletions(-) delete mode 100644 src/main/java/com/universe/uni/config/WebMvcConfig.java delete mode 100644 src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java diff --git a/src/main/java/com/universe/uni/config/WebMvcConfig.java b/src/main/java/com/universe/uni/config/WebMvcConfig.java deleted file mode 100644 index bf88d4a..0000000 --- a/src/main/java/com/universe/uni/config/WebMvcConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.universe.uni.config; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -import com.universe.uni.config.interceptor.AuthInterceptor; - -@Configuration -public class WebMvcConfig implements WebMvcConfigurer { - - @Autowired - private AuthInterceptor authInterceptor; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(authInterceptor); - } -} diff --git a/src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java b/src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java deleted file mode 100644 index 946c420..0000000 --- a/src/main/java/com/universe/uni/config/interceptor/AuthInterceptor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.universe.uni.config.interceptor; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.HandlerInterceptor; - -@Component -public class AuthInterceptor implements HandlerInterceptor { - - /* TODO 토큰변수 추가 */ - - private static final String AUTHORIZATION = "Authorization"; - - @Override - public boolean preHandle( - HttpServletRequest request, HttpServletResponse response, Object handler - ) throws Exception { - /* TODO 토큰 header exception 처리 */ - return true; - } -} From 1fb09d4d0fdea9e7741bcee804d9efa5ad04fe75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Tue, 4 Jul 2023 05:06:27 +0900 Subject: [PATCH 11/12] style: Fix to the coding convention --- .../controller/ControllerExceptionAdvice.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java b/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java index 7f86cd0..39c860d 100644 --- a/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java +++ b/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java @@ -28,7 +28,11 @@ public class ControllerExceptionAdvice extends ResponseEntityExceptionHandler { */ @Override protected ResponseEntity handleMethodArgumentNotValid( - MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { + MethodArgumentNotValidException exception, + HttpHeaders headers, + HttpStatus status, + WebRequest request + ) { ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } @@ -36,22 +40,27 @@ protected ResponseEntity handleMethodArgumentNotValid( @Override protected ResponseEntity handleMissingServletRequestParameter( MissingServletRequestParameterException exception, - HttpHeaders headers, HttpStatus status, WebRequest request) { + HttpHeaders headers, + HttpStatus status, + WebRequest request + ) { ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } @Override - protected ResponseEntity handleMissingPathVariable(MissingPathVariableException exception, - HttpHeaders headers, - HttpStatus status, WebRequest request) { + protected ResponseEntity handleMissingPathVariable( + MissingPathVariableException exception, + HttpHeaders headers, HttpStatus status, WebRequest request + ) { ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } @ExceptionHandler(MissingRequestHeaderException.class) protected ResponseEntity handleMissingRequestHeaderException( - MissingRequestHeaderException exception) { + MissingRequestHeaderException exception + ) { ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_EXCEPTION); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST); } @@ -61,7 +70,9 @@ protected ResponseEntity handleMissingRequestHeaderException( */ @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) - protected ErrorResponse handleException(final Exception exception) { + protected ErrorResponse handleException( + final Exception exception + ) { return ErrorResponse.error(ErrorType.INTERNAL_SERVER_ERROR); } @@ -69,8 +80,9 @@ protected ErrorResponse handleException(final Exception exception) { * Api custom error */ @ExceptionHandler(ApiException.class) - protected ResponseEntity handleCustomException(ApiException exception) { - return ResponseEntity.status(exception.getHttpStatus()) - .body(ErrorResponse.error(exception.getError())); + protected ResponseEntity handleCustomException( + ApiException exception + ) { + return ResponseEntity.status(exception.getHttpStatus()).body(ErrorResponse.error(exception.getError())); } } From de3c3de1fb8d546a26015a8c814ed8788a388c79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E1=84=8B=E1=85=B5=E1=84=88=E1=85=A1=E1=86=BC=E1=84=8C?= =?UTF-8?q?=E1=85=AE?= Date: Tue, 4 Jul 2023 05:07:49 +0900 Subject: [PATCH 12/12] style: Fix to the coding convention --- .../universe/uni/controller/ControllerExceptionAdvice.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java b/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java index 39c860d..24144fd 100644 --- a/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java +++ b/src/main/java/com/universe/uni/controller/ControllerExceptionAdvice.java @@ -51,7 +51,9 @@ protected ResponseEntity handleMissingServletRequestParameter( @Override protected ResponseEntity handleMissingPathVariable( MissingPathVariableException exception, - HttpHeaders headers, HttpStatus status, WebRequest request + HttpHeaders headers, + HttpStatus status, + WebRequest request ) { ErrorResponse errorResponse = ErrorResponse.error(ErrorType.VALIDATION_REQUEST_MISSING_EXCEPTION); return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);