File tree Expand file tree Collapse file tree 5 files changed +104
-0
lines changed
src/main/java/com/example/log4u/domain/comment Expand file tree Collapse file tree 5 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments