Skip to content

Commit 5aba692

Browse files
authored
Merge pull request #256 from june3780/refactoring_advice
Refactoring advice
2 parents 69ded54 + f3a3dae commit 5aba692

File tree

53 files changed

+279
-864
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+279
-864
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.grepp.spring.infra.error;
2+
3+
import com.grepp.spring.infra.error.exceptions.CustomException;
4+
import com.grepp.spring.infra.response.ApiResponse;
5+
import com.grepp.spring.infra.response.error.ErrorCode;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.core.annotation.Order;
8+
import org.springframework.http.ResponseEntity;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.RestControllerAdvice;
11+
12+
@Slf4j
13+
@RestControllerAdvice(basePackages = "com.grepp.spring.app.controller.api")
14+
@Order(0)
15+
public class ApiCustomExceptionAdvice {
16+
17+
@ExceptionHandler(CustomException.class)
18+
public ResponseEntity<ApiResponse<Object>> handleCustomException(CustomException ex) {
19+
ErrorCode errorCode = ex.getErrorCode();
20+
log.warn("API CustomException occurred: code={}, message={}", errorCode.code(), errorCode.message());
21+
22+
return ResponseEntity
23+
.status(errorCode.status())
24+
.body(ApiResponse.error(errorCode));
25+
}
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.grepp.spring.infra.error;
2+
3+
import com.grepp.spring.infra.response.ApiResponse;
4+
import com.grepp.spring.infra.response.ResponseCode;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.validation.BindException;
8+
import org.springframework.web.bind.MethodArgumentNotValidException;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.bind.annotation.RestControllerAdvice;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
@Slf4j
16+
@RestControllerAdvice
17+
public class GlobalExceptionAdvice {
18+
19+
@ExceptionHandler({MethodArgumentNotValidException.class, BindException.class})
20+
public ResponseEntity<ApiResponse<Map<String, String>>> validationExHandler(BindException ex) {
21+
Map<String, String> errors = new HashMap<>();
22+
ex.getBindingResult().getFieldErrors().forEach(error -> {
23+
String fieldName = error.getField();
24+
String errorMessage = error.getDefaultMessage();
25+
errors.put(fieldName, errorMessage);
26+
});
27+
log.warn("Validation error: {}", errors);
28+
return ResponseEntity
29+
.status(ResponseCode.BAD_REQUEST.status())
30+
.body(ApiResponse.error(ResponseCode.BAD_REQUEST, errors));
31+
}
32+
33+
@ExceptionHandler(IllegalArgumentException.class)
34+
public ResponseEntity<ApiResponse<String>> illegalArgumentExHandler(IllegalArgumentException ex) {
35+
log.warn("Illegal argument: {}", ex.getMessage());
36+
return ResponseEntity
37+
.status(ResponseCode.BAD_REQUEST.status())
38+
.body(ApiResponse.error(ResponseCode.BAD_REQUEST, ex.getMessage()));
39+
}
40+
41+
@ExceptionHandler(IllegalStateException.class)
42+
public ResponseEntity<ApiResponse<String>> illegalStateExHandler(IllegalStateException ex) {
43+
log.warn("Illegal state: {}", ex.getMessage());
44+
return ResponseEntity
45+
.status(ResponseCode.BAD_REQUEST.status())
46+
.body(ApiResponse.error(ResponseCode.BAD_REQUEST, ex.getMessage()));
47+
}
48+
49+
@ExceptionHandler(Exception.class)
50+
public ResponseEntity<ApiResponse<String>> allUncaughtExHandler(Exception ex) {
51+
log.error("Unhandled exception", ex);
52+
return ResponseEntity
53+
.status(ResponseCode.INTERNAL_SERVER_ERROR.status())
54+
.body(ApiResponse.error(ResponseCode.INTERNAL_SERVER_ERROR, "서버 내부 오류가 발생했습니다."));
55+
}
56+
}

src/main/java/com/grepp/spring/infra/error/eventAdvice/EventExceptionAdvice.java

Lines changed: 0 additions & 108 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.grepp.spring.infra.error.exceptions;
2+
3+
import com.grepp.spring.infra.response.error.ErrorCode;
4+
import lombok.Getter;
5+
6+
@Getter
7+
public abstract class CustomException extends RuntimeException {
8+
private final ErrorCode errorCode;
9+
10+
public CustomException(ErrorCode errorCode) {
11+
super(errorCode.message());
12+
this.errorCode = errorCode;
13+
}
14+
15+
public CustomException(ErrorCode errorCode, Exception e) {
16+
super(errorCode.message(), e);
17+
this.errorCode = errorCode;
18+
}
19+
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.grepp.spring.infra.error.exceptions.event;
22

3+
import com.grepp.spring.infra.error.exceptions.CustomException;
34
import com.grepp.spring.infra.response.EventErrorCode;
45
import lombok.extern.slf4j.Slf4j;
56

67
@Slf4j
7-
public class AlreadyConfirmedScheduleException extends RuntimeException {
8-
9-
private final EventErrorCode code;
8+
public class AlreadyConfirmedScheduleException extends CustomException {
109

1110
public AlreadyConfirmedScheduleException(EventErrorCode code) {
12-
this.code = code;
11+
super(code);
1312
}
1413

1514
public AlreadyConfirmedScheduleException(EventErrorCode code, Exception e) {
16-
this.code = code;
17-
log.error(e.getMessage(), e);
15+
super(code, e);
1816
}
1917

20-
public EventErrorCode code() {
21-
return code;
22-
}
2318
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.grepp.spring.infra.error.exceptions.event;
22

3+
import com.grepp.spring.infra.error.exceptions.CustomException;
34
import com.grepp.spring.infra.response.EventErrorCode;
45
import lombok.extern.slf4j.Slf4j;
56

67
@Slf4j
7-
public class AlreadyJoinedEventException extends RuntimeException {
8-
9-
private final EventErrorCode code;
8+
public class AlreadyJoinedEventException extends CustomException {
109

1110
public AlreadyJoinedEventException(EventErrorCode code) {
12-
this.code = code;
11+
super(code);
1312
}
1413

1514
public AlreadyJoinedEventException(EventErrorCode code, Exception e) {
16-
this.code = code;
17-
log.error(e.getMessage(), e);
15+
super(code, e);
1816
}
1917

20-
public EventErrorCode code() {
21-
return code;
22-
}
2318
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.grepp.spring.infra.error.exceptions.event;
22

3+
import com.grepp.spring.infra.error.exceptions.CustomException;
34
import com.grepp.spring.infra.response.EventErrorCode;
45
import lombok.extern.slf4j.Slf4j;
56

67
@Slf4j
7-
public class EventAlreadyCompletedException extends RuntimeException {
8-
9-
private final EventErrorCode code;
8+
public class EventAlreadyCompletedException extends CustomException {
109

1110
public EventAlreadyCompletedException(EventErrorCode code) {
12-
this.code = code;
11+
super(code);
1312
}
1413

1514
public EventAlreadyCompletedException(EventErrorCode code, Exception e) {
16-
this.code = code;
17-
log.error(e.getMessage(), e);
15+
super(code, e);
1816
}
1917

20-
public EventErrorCode code() {
21-
return code;
22-
}
2318
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.grepp.spring.infra.error.exceptions.event;
22

3+
import com.grepp.spring.infra.error.exceptions.CustomException;
34
import com.grepp.spring.infra.response.EventErrorCode;
45
import lombok.extern.slf4j.Slf4j;
56

67
@Slf4j
7-
public class EventAuthenticationException extends RuntimeException {
8-
9-
private final EventErrorCode code;
8+
public class EventAuthenticationException extends CustomException {
109

1110
public EventAuthenticationException(EventErrorCode code) {
12-
this.code = code;
11+
super(code);
1312
}
1413

1514
public EventAuthenticationException(EventErrorCode code, Exception e) {
16-
this.code = code;
17-
log.error(e.getMessage(), e);
15+
super(code, e);
1816
}
1917

20-
public EventErrorCode code() {
21-
return code;
22-
}
2318
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.grepp.spring.infra.error.exceptions.event;
22

3+
import com.grepp.spring.infra.error.exceptions.CustomException;
34
import com.grepp.spring.infra.response.EventErrorCode;
45
import lombok.extern.slf4j.Slf4j;
56

67
@Slf4j
7-
public class EventNotFoundException extends RuntimeException {
8-
9-
private final EventErrorCode code;
8+
public class EventNotFoundException extends CustomException {
109

1110
public EventNotFoundException(EventErrorCode code) {
12-
this.code = code;
11+
super(code);
1312
}
1413

1514
public EventNotFoundException(EventErrorCode code, Exception e) {
16-
this.code = code;
17-
log.error(e.getMessage(), e);
15+
super(code, e);
1816
}
1917

20-
public EventErrorCode code() {
21-
return code;
22-
}
2318
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
11
package com.grepp.spring.infra.error.exceptions.event;
22

3+
import com.grepp.spring.infra.error.exceptions.CustomException;
34
import com.grepp.spring.infra.response.EventErrorCode;
45
import lombok.extern.slf4j.Slf4j;
56

67
@Slf4j
7-
public class InvalidEventDataException extends RuntimeException {
8-
9-
private final EventErrorCode code;
8+
public class InvalidEventDataException extends CustomException {
109

1110
public InvalidEventDataException(EventErrorCode code) {
12-
this.code = code;
11+
super(code);
1312
}
1413

1514
public InvalidEventDataException(EventErrorCode code, Exception e) {
16-
this.code = code;
17-
log.error(e.getMessage(), e);
15+
super(code, e);
1816
}
1917

20-
public EventErrorCode code() {
21-
return code;
22-
}
2318
}

0 commit comments

Comments
 (0)