Skip to content

Commit 634f85a

Browse files
authored
Merge pull request #6 from UMC-9th-Spring-Boot/Feat/Chapter7
UMC 7주차 과제
2 parents 490bb0f + d84b375 commit 634f85a

File tree

10 files changed

+163
-0
lines changed

10 files changed

+163
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.UMC.domain.test.controller;
2+
3+
import com.example.UMC.domain.user.exception.code.UserErrorCode;
4+
import com.example.UMC.global.apiPayload.ApiResponse;
5+
import com.example.UMC.global.apiPayload.code.GeneralSucessCode;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
@RestController
11+
@RequestMapping("/test")
12+
public class TestController {
13+
14+
//성공
15+
@GetMapping("sucess")
16+
public ApiResponse<String> sucessTest() {
17+
return ApiResponse.onSucess(GeneralSucessCode.OK, "요청 정상 처리");
18+
}
19+
20+
//실패
21+
@GetMapping("fail")
22+
public ApiResponse<Void> failTest() {
23+
return ApiResponse.onFailure(UserErrorCode.USER_NOT_FOUND, null);
24+
}
25+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.UMC.domain.user.exception;
2+
3+
import com.example.UMC.global.apiPayload.code.BaseErrorCode;
4+
import com.example.UMC.global.apiPayload.exception.GeneralException;
5+
6+
public class UserException extends GeneralException {
7+
public UserException(BaseErrorCode code) {
8+
super(code);
9+
}
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.example.UMC.domain.user.exception.code;
2+
3+
import com.example.UMC.global.apiPayload.code.BaseErrorCode;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
import org.springframework.http.HttpStatus;
7+
8+
@Getter
9+
@AllArgsConstructor
10+
public enum UserErrorCode implements BaseErrorCode {
11+
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "USER4001", "해당 사용자를 찾을 수 없습니다."),
12+
DUPLICATE_EMAIL(HttpStatus.BAD_REQUEST, "USER4002","이미 존재하는 이메일입니다.");
13+
14+
private final HttpStatus status;
15+
private final String code;
16+
private final String message;
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.UMC.global.apiPayload;
2+
3+
import com.example.UMC.global.apiPayload.code.BaseErrorCode;
4+
import com.example.UMC.global.apiPayload.code.BaseSucessCode;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
8+
@Getter
9+
@AllArgsConstructor
10+
public class ApiResponse<T> {
11+
private final Boolean isSucess;
12+
private final String code;
13+
private final String message;
14+
private final T result;
15+
16+
// 성공 응답
17+
public static <T> ApiResponse<T> onSucess(BaseSucessCode code, T result){
18+
return new ApiResponse<>(true, code.getCode(), code.getMessage(), result);
19+
}
20+
21+
//실패 응답
22+
public static <T> ApiResponse<T> onFailure(BaseErrorCode code, T result) {
23+
return new ApiResponse<>(false, code.getCode(), code.getMessage(), result);
24+
}
25+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.example.UMC.global.apiPayload.code;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
public interface BaseErrorCode {
6+
HttpStatus getStatus();
7+
String getCode();
8+
String getMessage();
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.example.UMC.global.apiPayload.code;
2+
3+
public interface BaseSucessCode {
4+
String getCode();
5+
String getMessage();
6+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.example.UMC.global.apiPayload.code;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import org.springframework.http.HttpStatus;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public enum GeneralErrorCode implements BaseErrorCode {
10+
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "잘못된 요청입니다."),
11+
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "COMMON401", "인증이 필요합니다."),
12+
FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "접근이 거부되었습니다."),
13+
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "서버 오류가 발생했습니다.");
14+
15+
private final HttpStatus status;
16+
private final String code;
17+
private final String message;
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.example.UMC.global.apiPayload.code;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
6+
@Getter
7+
@AllArgsConstructor
8+
public enum GeneralSucessCode implements BaseSucessCode{
9+
OK("COMMON200", "요청이 성공적으로 처리되었습니다."),
10+
CREATED("COMMON201", "리소스가 성공적으로 생성되었습니다.");
11+
12+
private final String code;
13+
private final String message;
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.example.UMC.global.apiPayload.exception;
2+
3+
import com.example.UMC.global.apiPayload.code.BaseErrorCode;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public class GeneralException extends RuntimeException{
10+
11+
private final BaseErrorCode code;
12+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.example.UMC.global.apiPayload.handler;
2+
3+
import com.example.UMC.global.apiPayload.ApiResponse;
4+
import com.example.UMC.global.apiPayload.code.BaseErrorCode;
5+
import com.example.UMC.global.apiPayload.code.GeneralErrorCode;
6+
import com.example.UMC.global.apiPayload.exception.GeneralException;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.ExceptionHandler;
9+
import org.springframework.web.bind.annotation.RestControllerAdvice;
10+
11+
@RestControllerAdvice
12+
public class GeneralExceptionAdvice {
13+
14+
// 애플리케이션에서 발생하는 커스텀 예외를 처리(내가 만든 예외)
15+
@ExceptionHandler(GeneralException.class)
16+
public ResponseEntity<ApiResponse<Void>> handleException(GeneralException ex) {
17+
return ResponseEntity.status(ex.getCode().getStatus())
18+
.body(ApiResponse.onFailure(ex.getCode(), null));
19+
}
20+
// 그 외의 정의되지 않은 모든 예외 처리(스프링 내부에서 처리)
21+
@ExceptionHandler(Exception.class)
22+
public ResponseEntity<ApiResponse<String>> handleException(Exception ex) {
23+
BaseErrorCode code = GeneralErrorCode.INTERNAL_SERVER_ERROR;
24+
return ResponseEntity.status(code.getStatus())
25+
.body(ApiResponse.onFailure(code, ex.getMessage()));
26+
}
27+
}

0 commit comments

Comments
 (0)