-
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
[FEAT] Base Response ์ ํ
- Loading branch information
Showing
7 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/server/capple/global/common/BaseResponse.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,29 @@ | ||
package com.server.capple.global.common; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
import java.time.LocalDateTime; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@JsonPropertyOrder({"timeStamp", "code", "message", "result"}) | ||
public class BaseResponse<T> { | ||
|
||
private final LocalDateTime timeStamp = LocalDateTime.now(); | ||
private final String code; | ||
private final String message; | ||
@JsonInclude(Include.NON_NULL) | ||
private T result; | ||
|
||
// ์ฑ๊ณต ์ ์๋ต | ||
public static <T> BaseResponse<T> onSuccess(T result) { | ||
return new BaseResponse<>("COMMON200", "์์ฒญ์ ์ฑ๊ณตํ์์ต๋๋ค.", result); | ||
} | ||
|
||
public static <T> BaseResponse<T> onFailure(String code, String message, T data) { | ||
return new BaseResponse<>(code, message, data); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/main/java/com/server/capple/global/exception/ControllerAdvice.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,116 @@ | ||
package com.server.capple.global.exception; | ||
|
||
import com.server.capple.global.common.BaseResponse; | ||
import com.server.capple.global.exception.errorCode.GlobalErrorCode; | ||
import jakarta.validation.ConstraintViolationException; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; | ||
import org.springframework.web.servlet.NoHandlerFoundException; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@Slf4j | ||
@RestControllerAdvice(annotations = {RestController.class}) | ||
public class ControllerAdvice extends ResponseEntityExceptionHandler { | ||
|
||
/** | ||
* ์ ์ํ RestApiException ์์ธ ์ฒ๋ฆฌ | ||
*/ | ||
@ExceptionHandler(value = RestApiException.class) | ||
public ResponseEntity<BaseResponse<String>> handleRestApiException( | ||
RestApiException e) { | ||
ErrorCode errorCode = e.getErrorCode(); | ||
return handlerExceptionInternal(errorCode); | ||
} | ||
|
||
/** | ||
* ์ผ๋ฐ์ ์ธ ์๋ฒ ์๋ฌ ์์ธ ์ฒ๋ฆฌ | ||
*/ | ||
@ExceptionHandler | ||
public ResponseEntity<BaseResponse<String>> handleException( | ||
Exception e) { | ||
e.printStackTrace(); | ||
return handleExceptionInternalFalse(GlobalErrorCode.SERVER_ERROR.getErrorCode(), e.getMessage()); | ||
} | ||
|
||
/** | ||
* @Validated ๊ฒ์ฆ ์คํจ ์์ธ ์ฒ๋ฆฌ | ||
*/ | ||
@ExceptionHandler(ConstraintViolationException.class) | ||
public ResponseEntity<BaseResponse<String>> handleConstraintViolationException( | ||
ConstraintViolationException e) { | ||
return handleExceptionInternal(GlobalErrorCode.VALIDATION_ERROR.getErrorCode()); | ||
} | ||
|
||
/** | ||
* ๋ฉ์๋์ ์ธ์ ํ์ ์ด ์์๊ณผ ๋ค๋ฅธ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ | ||
*/ | ||
@ExceptionHandler(MethodArgumentTypeMismatchException.class) | ||
public ResponseEntity<BaseResponse<String>> handleMethodArgumentTypeMismatch( | ||
MethodArgumentTypeMismatchException e) { | ||
// ์์ธ ์ฒ๋ฆฌ ๋ก์ง | ||
return handleExceptionInternal(GlobalErrorCode.NOT_VALID_ARGUMENT_ERROR.getErrorCode()); | ||
} | ||
|
||
/** | ||
* @RequestBody ๋ด๋ถ ์ฒ๋ฆฌ ์คํจ, | ||
* @Valid ๊ฒ์ฆ ์คํจํ ๊ฒฝ์ฐ ์์ธ ์ฒ๋ฆฌ | ||
*/ | ||
@Override | ||
public ResponseEntity<Object> handleMethodArgumentNotValid( | ||
MethodArgumentNotValidException e, | ||
HttpHeaders headers, | ||
HttpStatusCode statusCode, | ||
WebRequest request) { | ||
Map<String, String> errors = new LinkedHashMap<>(); | ||
|
||
e.getBindingResult().getFieldErrors().stream() | ||
.forEach(fieldError -> { | ||
String fieldName = fieldError.getField(); | ||
String errorMessage = Optional.ofNullable(fieldError.getDefaultMessage()).orElse(""); | ||
errors.merge(fieldName, errorMessage, (existingErrorMessage, newErrorMessage) -> existingErrorMessage + ", " + newErrorMessage); | ||
}); | ||
|
||
return handleExceptionInternalArgs(GlobalErrorCode.VALIDATION_ERROR.getErrorCode(), errors); | ||
|
||
} | ||
|
||
private ResponseEntity<BaseResponse<String>> handlerExceptionInternal( | ||
ErrorCode errorCode) { | ||
return ResponseEntity | ||
.status(errorCode.getHttpStatus().value()) | ||
.body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), null)); | ||
} | ||
|
||
private ResponseEntity<BaseResponse<String>> handleExceptionInternal(ErrorCode errorCode) { | ||
return ResponseEntity | ||
.status(errorCode.getHttpStatus().value()) | ||
.body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), null)); | ||
} | ||
|
||
private ResponseEntity<Object> handleExceptionInternalArgs( | ||
ErrorCode errorCode, | ||
Map<String, String> errorArgs) { | ||
return ResponseEntity | ||
.status(errorCode.getHttpStatus().value()) | ||
.body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), errorArgs)); | ||
} | ||
private ResponseEntity<BaseResponse<String>> handleExceptionInternalFalse( | ||
ErrorCode errorCode, | ||
String errorPoint) { | ||
return ResponseEntity | ||
.status(errorCode.getHttpStatus().value()) | ||
.body(BaseResponse.onFailure(errorCode.getCode(), errorCode.getMessage(), errorPoint)); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/server/capple/global/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,13 @@ | ||
package com.server.capple.global.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@Builder | ||
public class ErrorCode { | ||
private final String code; | ||
private final String message; | ||
private final HttpStatus httpStatus; | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/server/capple/global/exception/ErrorCodeInterface.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,6 @@ | ||
package com.server.capple.global.exception; | ||
|
||
|
||
public interface ErrorCodeInterface { | ||
ErrorCode getErrorCode(); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/server/capple/global/exception/RestApiException.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.server.capple.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class RestApiException extends RuntimeException { | ||
private final ErrorCodeInterface errorCode; | ||
|
||
public ErrorCode getErrorCode() { | ||
return this.errorCode.getErrorCode(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/server/capple/global/exception/errorCode/GlobalErrorCode.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,34 @@ | ||
package com.server.capple.global.exception.errorCode; | ||
|
||
import com.server.capple.global.exception.ErrorCode; | ||
import com.server.capple.global.exception.ErrorCodeInterface; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum GlobalErrorCode implements ErrorCodeInterface { | ||
BAD_REQUEST("GLOBAL001", "์๋ชป๋ ์์ฒญ์ ๋๋ค.", HttpStatus.BAD_REQUEST), | ||
NOT_SUPPORTED_URI_ERROR("GLOBAL002", "์ฌ๋ฐ๋ฅด์ง ์์ URI์ ๋๋ค.", HttpStatus.NOT_FOUND), | ||
NOT_SUPPORTED_METHOD_ERROR("GLOBAL003", "์ง์ํ์ง ์๋ Method์ ๋๋ค.", HttpStatus.METHOD_NOT_ALLOWED), | ||
NOT_SUPPORTED_MEDIA_TYPE_ERROR("GLOBAL004", "์ง์ํ์ง ์๋ Media type์ ๋๋ค.", HttpStatus.UNSUPPORTED_MEDIA_TYPE), | ||
SERVER_ERROR("GLOBAL005", "์๋ฒ ์๋ฌ, ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํด์ฃผ์ธ์.", HttpStatus.INTERNAL_SERVER_ERROR), | ||
ACCESS_DENIED("GLOBAL006", "์ฌ๋ฐ๋ฅด์ง ์์ ๊ถํ์ ๋๋ค.", HttpStatus.FORBIDDEN), | ||
NOT_VALID_ARGUMENT_ERROR("GLOBAL007", "์ฌ๋ฐ๋ฅด์ง ์์ Argument Type์ ๋๋ค.", HttpStatus.BAD_REQUEST), | ||
VALIDATION_ERROR("GLOBAL007", "Validation Error์ ๋๋ค.", HttpStatus.BAD_REQUEST), | ||
; | ||
|
||
private final String code; | ||
private final String message; | ||
private final HttpStatus httpStatus; | ||
|
||
@Override | ||
public ErrorCode getErrorCode() { | ||
return ErrorCode.builder() | ||
.code(code) | ||
.message(message) | ||
.httpStatus(httpStatus) | ||
.build(); | ||
} | ||
} |