-
Notifications
You must be signed in to change notification settings - Fork 2
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
[setting] 에러 및 응답관련 초기셋팅
- Loading branch information
Showing
13 changed files
with
247 additions
and
5 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
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 +1 @@ | ||
rootProject.name = 'server' | ||
rootProject.name = 'PickpleServer' |
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
Empty file.
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,57 @@ | ||
package com.pickple.server.api; | ||
|
||
import com.pickple.server.global.exception.CustomException; | ||
import com.pickple.server.global.response.enums.ErrorCode; | ||
import com.pickple.server.global.response.ApiResponse; | ||
import com.pickple.server.global.response.enums.SuccessCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/test") | ||
public class TestController { | ||
|
||
@GetMapping("/responseEntity") | ||
public ResponseEntity<String> responseEntity() { | ||
return ResponseEntity.ok("ok"); | ||
} | ||
|
||
@GetMapping("/success1") | ||
public ApiResponse<String> successWithData() { | ||
return ApiResponse.success(SuccessCode.TEST_OK_SUCCESS, "data"); | ||
} | ||
|
||
@GetMapping("/success2") | ||
public ApiResponse<Void> successWithoutData() { | ||
return ApiResponse.success(SuccessCode.TEST_CREATE_SUCCESS); | ||
} | ||
|
||
|
||
@GetMapping("/exception1") | ||
public ApiResponse<Void> testCustomException() { | ||
return ApiResponse.fail(ErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@GetMapping("/exception2") | ||
public ApiResponse<Void> testException() { | ||
throw new RuntimeException("This is a test exception"); | ||
} | ||
|
||
@ExceptionHandler(CustomException.class) | ||
public ResponseEntity<ApiResponse<Void>> handleCustomException(CustomException ex) { | ||
ApiResponse<Void> response = ApiResponse.fail(ex.getErrorCode()); | ||
return new ResponseEntity<>(response, ex.getErrorCode().getHttpStatus()); | ||
} | ||
|
||
@ExceptionHandler(RuntimeException.class) | ||
public ResponseEntity<ApiResponse<Void>> handleRuntimeException(RuntimeException ex) { | ||
ApiResponse<Void> response = ApiResponse.fail(ErrorCode.TEST_ERROR); | ||
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/pickple/server/global/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,19 @@ | ||
package com.pickple.server.global.exception; | ||
|
||
import com.pickple.server.global.response.enums.ErrorCode; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class CustomException extends RuntimeException{ | ||
private final ErrorCode errorCode; | ||
|
||
public CustomException(ErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
|
||
public HttpStatus getHttpStatus() { | ||
return errorCode.getHttpStatus(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/pickple/server/global/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 |
---|---|---|
@@ -1,4 +1,40 @@ | ||
package com.pickple.server.global.exception; | ||
|
||
import com.pickple.server.global.response.enums.ErrorCode; | ||
import com.pickple.server.global.response.ApiResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.HttpRequestMethodNotSupportedException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.NoHandlerFoundException; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
// 존재하지 않는 요청에 대한 예외 | ||
@ExceptionHandler(value = {NoHandlerFoundException.class, HttpRequestMethodNotSupportedException.class}) | ||
public ApiResponse<?> handleNoPageFoundException(Exception e) { | ||
log.error("GlobalExceptionHandler catch NoHandlerFoundException : {}", e.getMessage()); | ||
return ApiResponse.fail(ErrorCode.NOT_FOUND_END_POINT); | ||
} | ||
|
||
// 기본 예외 | ||
@ExceptionHandler(value = {Exception.class}) | ||
public ApiResponse<?> handleException(Exception e) { | ||
log.error("handleException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); | ||
e.printStackTrace(); | ||
return ApiResponse.fail(ErrorCode.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
//@Slf4j | ||
//@RestControllerAdvice | ||
//public class GlobalExceptionHandler { | ||
// // 비즈니스 로직에서 발생한 예외 | ||
// @ExceptionHandler(CustomException.class) | ||
// public ResponseEntity<ErrorCode> handleBusinessException(CustomException e) { | ||
// log.error("GlobalExceptionHandler catch CustomException : {}", e.getErrorCode().getMessage()); | ||
// return ResponseEntity | ||
// .status(e.getErrorCode().getHttpStatus()) | ||
// .body(e.getErrorCode()); | ||
// } | ||
//} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/pickple/server/global/exception/ResponseStatusSetterAdvice.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,35 @@ | ||
package com.pickple.server.global.exception; | ||
|
||
import com.pickple.server.global.response.ApiResponse; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
|
||
@RestControllerAdvice | ||
public class ResponseStatusSetterAdvice implements ResponseBodyAdvice<ApiResponse<?>> { | ||
|
||
@Override | ||
public boolean supports(MethodParameter returnType, Class converterType) { | ||
return returnType.getParameterType() == ApiResponse.class; | ||
} | ||
|
||
@Override | ||
public ApiResponse<?> beforeBodyWrite( | ||
ApiResponse body, | ||
MethodParameter returnType, | ||
MediaType selectedContentType, | ||
Class selectedConverterType, | ||
ServerHttpRequest request, | ||
ServerHttpResponse response | ||
) { | ||
HttpStatus status = body.httpStatus(); | ||
response.setStatusCode(status); | ||
|
||
return body; | ||
} | ||
} |
Empty file.
45 changes: 45 additions & 0 deletions
45
src/main/java/com/pickple/server/global/response/ApiResponse.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,45 @@ | ||
package com.pickple.server.global.response; | ||
|
||
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.pickple.server.global.response.enums.ErrorCode; | ||
import com.pickple.server.global.response.enums.SuccessCode; | ||
import jakarta.annotation.Nullable; | ||
import lombok.Builder; | ||
import lombok.NonNull; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Builder | ||
public record ApiResponse<T> ( | ||
@JsonIgnore HttpStatus httpStatus, | ||
int status , | ||
@NonNull String message, | ||
@JsonInclude(value = NON_NULL) T data | ||
){ | ||
public static<T> ApiResponse<T> success(final SuccessCode successCode, @Nullable final T data) { | ||
return ApiResponse.<T>builder() | ||
.httpStatus(successCode.getHttpStatus()) | ||
.status(successCode.getCode()) | ||
.message(successCode.getMessage()) | ||
.data(data) | ||
.build(); | ||
} | ||
public static <T> ApiResponse<T> success(final SuccessCode successCode) { | ||
return ApiResponse.<T>builder() | ||
.httpStatus(successCode.getHttpStatus()) | ||
.status(successCode.getCode()) | ||
.message(successCode.getMessage()) | ||
.data(null) | ||
.build(); | ||
} | ||
public static <T> ApiResponse<T> fail(final ErrorCode errorCode) { | ||
return ApiResponse.<T>builder() | ||
.httpStatus(errorCode.getHttpStatus()) | ||
.status(errorCode.getCode()) | ||
.message(errorCode.getMessage()) | ||
.data(null) | ||
.build(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/pickple/server/global/response/enums/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.pickple.server.global.response.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorCode { | ||
// Test Error | ||
TEST_ERROR(10000, HttpStatus.BAD_REQUEST, "테스트 에러입니다."), | ||
// 404 Not Found | ||
NOT_FOUND_END_POINT(40400, HttpStatus.NOT_FOUND, "존재하지 않는 API입니다."), | ||
// 500 Internal Server Error | ||
INTERNAL_SERVER_ERROR(50000, HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류입니다."); | ||
|
||
private final int code; | ||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/pickple/server/global/response/enums/SuccessCode.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,16 @@ | ||
package com.pickple.server.global.response.enums; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum SuccessCode { | ||
TEST_OK_SUCCESS(20000, HttpStatus.OK, "test 조회 성공"), | ||
TEST_CREATE_SUCCESS(20100, HttpStatus.CREATED, "test 생성 성공"); | ||
|
||
private final int code; | ||
private final HttpStatus httpStatus; | ||
private final String message; | ||
} |