Skip to content

Commit 5c50c2d

Browse files
committed
feat: 도메인 예외 사용 예시로 Comment 도메인 Exception 구조 예시 추가
1 parent ac43355 commit 5c50c2d

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.example.log4u.domain.comment.exception;
2+
3+
import org.springframework.http.HttpStatus;
4+
5+
import com.example.log4u.common.exception.base.ErrorCode;
6+
7+
import lombok.Getter;
8+
import lombok.RequiredArgsConstructor;
9+
10+
@Getter
11+
@RequiredArgsConstructor
12+
public enum CommentErrorCode implements ErrorCode {
13+
14+
NOT_FOUND_COMMENT(HttpStatus.NOT_FOUND, "댓글을 찾을 수 없습니다.");
15+
16+
private final HttpStatus httpStatus;
17+
private final String message;
18+
19+
@Override
20+
public HttpStatus getHttpStatus() {
21+
return httpStatus;
22+
}
23+
24+
@Override
25+
public String getErrorMessage() {
26+
return message;
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.example.log4u.domain.comment.exception;
2+
3+
import com.example.log4u.common.exception.base.ErrorCode;
4+
import com.example.log4u.common.exception.base.ServiceException;
5+
6+
public class CommentException extends ServiceException {
7+
public CommentException(ErrorCode errorCode) {
8+
super(errorCode);
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.example.log4u.domain.comment.exception;
2+
3+
public class NotFoundCommentException extends CommentException {
4+
public NotFoundCommentException() {
5+
super(CommentErrorCode.NOT_FOUND_COMMENT);
6+
}
7+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.log4u.domain.comment.testController;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PostMapping;
6+
import org.springframework.web.bind.annotation.RequestBody;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RestController;
9+
10+
import com.example.log4u.domain.comment.testDto.TestRequest;
11+
import com.example.log4u.domain.comment.exception.NotFoundCommentException;
12+
13+
import jakarta.validation.Valid;
14+
15+
@RestController
16+
@RequestMapping("/test")
17+
public class TestController {
18+
19+
@PostMapping("/valid")
20+
public ResponseEntity<Void> testValidation(@RequestBody @Valid TestRequest request) {
21+
return ResponseEntity.ok().build();
22+
}
23+
24+
@GetMapping("/illegal")
25+
public String testIllegalArgument() {
26+
throw new IllegalArgumentException("잘못된 인자입니다!");
27+
}
28+
29+
@GetMapping("/log4u")
30+
public String testLog4uException() {
31+
throw new NotFoundCommentException(); // 또는 임의의 ServiceException
32+
}
33+
34+
@GetMapping("/unknown")
35+
public String testUnexpectedException() {
36+
String str = null;
37+
str.length(); // NPE
38+
return "절대 도달하지 않음";
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.example.log4u.domain.comment.testDto;
2+
3+
// dto/TestRequest.java
4+
import jakarta.validation.constraints.Min;
5+
import jakarta.validation.constraints.NotBlank;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
9+
@Getter
10+
@AllArgsConstructor
11+
public class TestRequest {
12+
13+
@NotBlank
14+
private String name;
15+
16+
@Min(value = 18, message = "나이는 18세 이상이어야 합니다.")
17+
private int age;
18+
19+
}

0 commit comments

Comments
 (0)