|
1 | 1 | package com.ai.lawyer.global.exception; |
2 | 2 |
|
3 | 3 | import com.ai.lawyer.global.exception.ServiceException; |
4 | | -import com.ai.lawyer.global.rsData.RsData; |
5 | 4 | import jakarta.validation.ConstraintViolationException; |
6 | 5 | import lombok.RequiredArgsConstructor; |
7 | 6 | import org.springframework.http.ResponseEntity; |
|
23 | 22 | @RestControllerAdvice |
24 | 23 | @RequiredArgsConstructor |
25 | 24 | public class GlobalExceptionHandler { |
26 | | - @ExceptionHandler(NoSuchElementException.class) |
27 | | - public ResponseEntity<RsData<Void>> handle(NoSuchElementException ex) { |
28 | | - return new ResponseEntity<>( |
29 | | - new RsData<>( |
30 | | - "404-1", |
31 | | - "해당 데이터가 존재하지 않습니다." |
32 | | - ), |
33 | | - NOT_FOUND |
34 | | - ); |
35 | | - } |
36 | 25 |
|
37 | | - |
38 | | - @ExceptionHandler(ConstraintViolationException.class) |
39 | | - public ResponseEntity<RsData<Void>> handle(ConstraintViolationException ex) { |
40 | | - String message = ex.getConstraintViolations() |
41 | | - .stream() |
42 | | - .map(violation -> { |
43 | | - String field = violation.getPropertyPath().toString().split("\\.", 2)[1]; |
44 | | - String[] messageTemplateBits = violation.getMessageTemplate() |
45 | | - .split("\\."); |
46 | | - String code = messageTemplateBits[messageTemplateBits.length - 2]; |
47 | | - String _message = violation.getMessage(); |
48 | | - |
49 | | - return "%s-%s-%s".formatted(field, code, _message); |
50 | | - }) |
51 | | - .sorted(Comparator.comparing(String::toString)) |
52 | | - .collect(Collectors.joining("\n")); |
53 | | - |
54 | | - return new ResponseEntity<>( |
55 | | - new RsData<>( |
56 | | - "400-1", |
57 | | - message |
58 | | - ), |
59 | | - BAD_REQUEST |
60 | | - ); |
61 | | - } |
62 | | - |
63 | | - @ExceptionHandler(MethodArgumentNotValidException.class) |
64 | | - public ResponseEntity<RsData<Void>> handle(MethodArgumentNotValidException ex) { |
65 | | - String message = ex.getBindingResult() |
66 | | - .getAllErrors() |
67 | | - .stream() |
68 | | - .filter(error -> error instanceof FieldError) |
69 | | - .map(error -> (FieldError) error) |
70 | | - .map(error -> error.getField() + "-" + error.getCode() + "-" + error.getDefaultMessage()) |
71 | | - .sorted(Comparator.comparing(String::toString)) |
72 | | - .collect(Collectors.joining("\n")); |
73 | | - |
74 | | - return new ResponseEntity<>( |
75 | | - new RsData<>( |
76 | | - "400-1", |
77 | | - message |
78 | | - ), |
79 | | - BAD_REQUEST |
80 | | - ); |
81 | | - } |
82 | | - |
83 | | - @ExceptionHandler(HttpMessageNotReadableException.class) |
84 | | - public ResponseEntity<RsData<Void>> handle(HttpMessageNotReadableException ex) { |
85 | | - return new ResponseEntity<>( |
86 | | - new RsData<>( |
87 | | - "400-1", |
88 | | - "요청 본문이 올바르지 않습니다." |
89 | | - ), |
90 | | - BAD_REQUEST |
91 | | - ); |
92 | | - } |
93 | | - |
94 | | - @ExceptionHandler(MissingRequestHeaderException.class) |
95 | | - public ResponseEntity<RsData<Void>> handle(MissingRequestHeaderException ex) { |
96 | | - return new ResponseEntity<>( |
97 | | - new RsData<>( |
98 | | - "400-1", |
99 | | - "%s-%s-%s".formatted( |
100 | | - ex.getHeaderName(), |
101 | | - "NotBlank", |
102 | | - ex.getLocalizedMessage() |
103 | | - ) |
104 | | - ), |
105 | | - BAD_REQUEST |
106 | | - ); |
107 | | - } |
108 | | - |
109 | | - @ExceptionHandler(ServiceException.class) |
110 | | - @ResponseStatus(BAD_REQUEST) |
111 | | - public ResponseEntity<RsData<Void>> handle(ServiceException ex) { |
112 | | - RsData<Void> rsData = ex.getRsData(); |
113 | | - |
114 | | - return new ResponseEntity<>( |
115 | | - rsData, |
116 | | - ResponseEntity |
117 | | - .status(rsData.statusCode()) |
118 | | - .build() |
119 | | - .getStatusCode() |
120 | | - ); |
121 | | - } |
122 | 26 | } |
0 commit comments