-
Notifications
You must be signed in to change notification settings - Fork 0
UMC 7주차 과제 #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
UMC 7주차 과제 #6
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/com/example/UMC/domain/test/controller/TestController.java
This file contains hidden or 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,25 @@ | ||
| package com.example.UMC.domain.test.controller; | ||
|
|
||
| import com.example.UMC.domain.user.exception.code.UserErrorCode; | ||
| import com.example.UMC.global.apiPayload.ApiResponse; | ||
| import com.example.UMC.global.apiPayload.code.GeneralSucessCode; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/test") | ||
| public class TestController { | ||
|
|
||
| //성공 | ||
| @GetMapping("sucess") | ||
| public ApiResponse<String> sucessTest() { | ||
| return ApiResponse.onSucess(GeneralSucessCode.OK, "요청 정상 처리"); | ||
| } | ||
|
|
||
| //실패 | ||
| @GetMapping("fail") | ||
| public ApiResponse<Void> failTest() { | ||
| return ApiResponse.onFailure(UserErrorCode.USER_NOT_FOUND, null); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/UMC/domain/user/exception/UserException.java
This file contains hidden or 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.example.UMC.domain.user.exception; | ||
|
|
||
| import com.example.UMC.global.apiPayload.code.BaseErrorCode; | ||
| import com.example.UMC.global.apiPayload.exception.GeneralException; | ||
|
|
||
| public class UserException extends GeneralException { | ||
| public UserException(BaseErrorCode code) { | ||
| super(code); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/UMC/domain/user/exception/code/UserErrorCode.java
This file contains hidden or 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,17 @@ | ||
| package com.example.UMC.domain.user.exception.code; | ||
|
|
||
| import com.example.UMC.global.apiPayload.code.BaseErrorCode; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum UserErrorCode implements BaseErrorCode { | ||
| USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER4001", "해당 사용자를 찾을 수 없습니다."), | ||
| DUPLICATE_EMAIL(HttpStatus.BAD_REQUEST, "USER4002","이미 존재하는 이메일입니다."); | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
| } |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/example/UMC/global/apiPayload/ApiResponse.java
This file contains hidden or 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,25 @@ | ||
| package com.example.UMC.global.apiPayload; | ||
|
|
||
| import com.example.UMC.global.apiPayload.code.BaseErrorCode; | ||
| import com.example.UMC.global.apiPayload.code.BaseSucessCode; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class ApiResponse<T> { | ||
| private final Boolean isSucess; | ||
| private final String code; | ||
| private final String message; | ||
| private final T result; | ||
|
|
||
| // 성공 응답 | ||
| public static <T> ApiResponse<T> onSucess(BaseSucessCode code, T result){ | ||
| return new ApiResponse<>(true, code.getCode(), code.getMessage(), result); | ||
| } | ||
|
|
||
| //실패 응답 | ||
| public static <T> ApiResponse<T> onFailure(BaseErrorCode code, T result) { | ||
| return new ApiResponse<>(false, code.getCode(), code.getMessage(), result); | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/example/UMC/global/apiPayload/code/BaseErrorCode.java
This file contains hidden or 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,9 @@ | ||
| package com.example.UMC.global.apiPayload.code; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| public interface BaseErrorCode { | ||
| HttpStatus getStatus(); | ||
| String getCode(); | ||
| String getMessage(); | ||
| } |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/example/UMC/global/apiPayload/code/BaseSucessCode.java
This file contains hidden or 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.example.UMC.global.apiPayload.code; | ||
|
|
||
| public interface BaseSucessCode { | ||
| String getCode(); | ||
| String getMessage(); | ||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/UMC/global/apiPayload/code/GeneralErrorCode.java
This file contains hidden or 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,18 @@ | ||
| package com.example.UMC.global.apiPayload.code; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
| import org.springframework.http.HttpStatus; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum GeneralErrorCode implements BaseErrorCode { | ||
| BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."), | ||
| UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."), | ||
| FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "접근이 거부되었습니다."), | ||
| INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "서버 오류가 발생했습니다."); | ||
|
|
||
| private final HttpStatus status; | ||
| private final String code; | ||
| private final String message; | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/UMC/global/apiPayload/code/GeneralSucessCode.java
This file contains hidden or 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.example.UMC.global.apiPayload.code; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public enum GeneralSucessCode implements BaseSucessCode{ | ||
| OK("COMMON200", "요청이 성공적으로 처리되었습니다."), | ||
| CREATED("COMMON201", "리소스가 성공적으로 생성되었습니다."); | ||
|
|
||
| private final String code; | ||
| private final String message; | ||
| } |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/UMC/global/apiPayload/exception/GeneralException.java
This file contains hidden or 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.example.UMC.global.apiPayload.exception; | ||
|
|
||
| import com.example.UMC.global.apiPayload.code.BaseErrorCode; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor | ||
| public class GeneralException extends RuntimeException{ | ||
|
|
||
| private final BaseErrorCode code; | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/example/UMC/global/apiPayload/handler/GeneralExceptionAdvice.java
This file contains hidden or 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,27 @@ | ||
| package com.example.UMC.global.apiPayload.handler; | ||
|
|
||
| import com.example.UMC.global.apiPayload.ApiResponse; | ||
| import com.example.UMC.global.apiPayload.code.BaseErrorCode; | ||
| import com.example.UMC.global.apiPayload.code.GeneralErrorCode; | ||
| import com.example.UMC.global.apiPayload.exception.GeneralException; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
|
||
| @RestControllerAdvice | ||
| public class GeneralExceptionAdvice { | ||
|
|
||
| // 애플리케이션에서 발생하는 커스텀 예외를 처리(내가 만든 예외) | ||
| @ExceptionHandler(GeneralException.class) | ||
| public ResponseEntity<ApiResponse<Void>> handleException(GeneralException ex) { | ||
| return ResponseEntity.status(ex.getCode().getStatus()) | ||
| .body(ApiResponse.onFailure(ex.getCode(), null)); | ||
| } | ||
| // 그 외의 정의되지 않은 모든 예외 처리(스프링 내부에서 처리) | ||
| @ExceptionHandler(Exception.class) | ||
| public ResponseEntity<ApiResponse<String>> handleException(Exception ex) { | ||
| BaseErrorCode code = GeneralErrorCode.INTERNAL_SERVER_ERROR; | ||
| return ResponseEntity.status(code.getStatus()) | ||
| .body(ApiResponse.onFailure(code, ex.getMessage())); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
우리 열심히 만들었던 공통응답을 활용해보아요 😢