Skip to content

Commit 77681fe

Browse files
committed
feat: 예외 처리 로직 구현
1 parent 882c58a commit 77681fe

File tree

7 files changed

+127
-0
lines changed

7 files changed

+127
-0
lines changed

slidingwindowlog.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module version="4">
3+
<component name="AdditionalModuleElements">
4+
<content url="file://$MODULE_DIR$" dumb="true">
5+
<excludeFolder url="file://$MODULE_DIR$/.idea/copilot/chatSessions" />
6+
</content>
7+
</component>
8+
<component name="SonarLintModuleSettings">
9+
<option name="uniqueId" value="5f9c5e5f-cc45-4d93-afba-2fbb968908fa" />
10+
</component>
11+
</module>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.systemdesign.slidingwindowlog.common.dto;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@Getter
7+
@RequiredArgsConstructor(staticName = "from")
8+
public class ExceptionResponse {
9+
10+
private final String code;
11+
}
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.systemdesign.slidingwindowlog.common.exception;
2+
3+
import lombok.Getter;
4+
5+
@Getter
6+
public abstract class BusinessException extends RuntimeException{
7+
8+
private final ExceptionCode exceptionCode;
9+
private final Object[] rejectedValues;
10+
11+
protected BusinessException(ExceptionCode exceptionCode, Object... rejectedValues) {
12+
super(exceptionCode.getMessage());
13+
this.exceptionCode = exceptionCode;
14+
this.rejectedValues = rejectedValues;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.systemdesign.slidingwindowlog.common.exception;
2+
3+
public class CommonException extends BusinessException {
4+
5+
public CommonException(ExceptionCode exceptionCode, Object... rejectedValues) {
6+
super(exceptionCode, rejectedValues);
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.systemdesign.slidingwindowlog.common.exception;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
import org.springframework.http.HttpStatus;
6+
7+
import static org.springframework.http.HttpStatus.*;
8+
9+
@Getter
10+
@RequiredArgsConstructor
11+
public enum CommonExceptionCode implements ExceptionCode {
12+
13+
COMMON_NOT_FOUND(NOT_FOUND, "COM-001", "요청한 URL에 해당하는 리소스를 찾을 수 없음"),
14+
COMMON_BAD_REQUEST(BAD_REQUEST, "COM-002", "잘못된 요청"),
15+
COMMON_METHOD_NOT_ALLOWED(METHOD_NOT_ALLOWED, "COM-003", "허용되지 않은 HTTP Method 요청 발생"),
16+
COMMON_INTERNAL_SERVER_ERROR(INTERNAL_SERVER_ERROR, "COM-004", "기타 서버 내부 에러 발생"),
17+
;
18+
19+
private final HttpStatus status;
20+
private final String code;
21+
private final String message;
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.systemdesign.slidingwindowlog.common.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
public interface ExceptionCode {
6+
7+
HttpStatus getStatus();
8+
9+
String getCode();
10+
11+
String getMessage();
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.systemdesign.slidingwindowlog.common.exception;
2+
3+
import com.systemdesign.slidingwindowlog.common.dto.ExceptionResponse;
4+
import org.springframework.http.ResponseEntity;
5+
import org.springframework.web.HttpRequestMethodNotSupportedException;
6+
import org.springframework.web.bind.MethodArgumentNotValidException;
7+
import org.springframework.web.bind.annotation.ExceptionHandler;
8+
import org.springframework.web.bind.annotation.RestControllerAdvice;
9+
import org.springframework.web.servlet.NoHandlerFoundException;
10+
11+
import static com.systemdesign.slidingwindowlog.common.exception.CommonExceptionCode.*;
12+
13+
@RestControllerAdvice
14+
public class GlobalExceptionHandler {
15+
16+
@ExceptionHandler(NoHandlerFoundException.class)
17+
public ResponseEntity<ExceptionResponse> handleNoHandlerFoundException(NoHandlerFoundException e) {
18+
return ResponseEntity.status(COMMON_NOT_FOUND.getStatus())
19+
.body(ExceptionResponse.from(COMMON_NOT_FOUND.getCode()));
20+
}
21+
22+
@ExceptionHandler(MethodArgumentNotValidException.class)
23+
public ResponseEntity<ExceptionResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
24+
return ResponseEntity.status(COMMON_BAD_REQUEST.getStatus())
25+
.body(ExceptionResponse.from(COMMON_BAD_REQUEST.getCode()));
26+
}
27+
28+
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
29+
public ResponseEntity<ExceptionResponse> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
30+
return ResponseEntity.status(COMMON_METHOD_NOT_ALLOWED.getStatus())
31+
.body(ExceptionResponse.from(COMMON_METHOD_NOT_ALLOWED.getCode()));
32+
}
33+
34+
@ExceptionHandler(Exception.class)
35+
public ResponseEntity<ExceptionResponse> handleException(Exception e) {
36+
return ResponseEntity.status(COMMON_INTERNAL_SERVER_ERROR.getStatus())
37+
.body(ExceptionResponse.from(COMMON_INTERNAL_SERVER_ERROR.getCode()));
38+
}
39+
40+
@ExceptionHandler(BusinessException.class)
41+
public ResponseEntity<ExceptionResponse> handleBusinessException(BusinessException e) {
42+
ExceptionCode exceptionCode = e.getExceptionCode();
43+
return ResponseEntity.status(exceptionCode.getStatus())
44+
.body(ExceptionResponse.from(exceptionCode.getCode()));
45+
}
46+
}

0 commit comments

Comments
 (0)