Skip to content

Commit a71cdd2

Browse files
authored
Merge 0.3.0 to develop (#23)
2 parents 278ccac + 129a912 commit a71cdd2

22 files changed

Lines changed: 283 additions & 328 deletions

api-payload/README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ Because DefaultResponseWriteUtil and DefaultResponseWriter is already registered
1313
public class ExceptionAdviceConfig {
1414

1515
@Bean
16-
ExceptionAdviceConfigurer exceptionAdviceConfigurer(ExceptionHandlerRegistry exceptionHandlerRegistry) {
17-
ExceptionAdviceConfigurer exceptionAdviceConfigurer = new ExceptionAdviceConfigurer(exceptionHandlerRegistry);
18-
return exceptionAdviceConfigurer
19-
.withDefault()
20-
.enableMethodArgumentNotValidExceptionAdvice(DefaultResponseErrorCode._BAD_REQUEST)
21-
.enableConstraintViolationExceptionAdvice(DefaultResponseErrorCode._BAD_REQUEST)
22-
.enableGlobalExceptionAdvice(DefaultResponseErrorCode._INTERNAL_SERVER_ERROR)
23-
;
16+
ExceptionAdviceConfigurer<DefaultResponseErrorReasonDTO> defaultExceptionAdviceConfigurer(FailureResponseWriter<DefaultResponseErrorReasonDTO> failureResponseWriter) {
17+
return new DefaultExceptionAdviceConfigurer(failureResponseWriter);
2418
}
2519
}
2620
```
@@ -51,4 +45,26 @@ You can make custom response by implementing BaseResponse, ErrorReasonDTO, Succe
5145
1. Make custom BaseResponse
5246
2. Make custom ErrorReasonDTO, SuccessReasonDTO
5347
3. Make custom SuccessResponseWriter, FailureResponseWriter
54-
4. Register your custom Util, Handler, Writer in bean
48+
4. Register your custom Handler, Writer in bean
49+
50+
### how to add custom handler in ExceptionAdvice
51+
```java
52+
@Configuration
53+
@RequiredArgsConstructor
54+
public class ExceptionAdviceConfig {
55+
56+
private final CustomFailureResponseWriter customFailureResponseWriter;
57+
private final CustomServerApplicationExceptionHandler customServerApplicationExceptionHandler;
58+
59+
@Bean
60+
ExceptionAdviceConfigurer<CustomErrorReasonDTO> exceptionAdviceConfigurer() {
61+
ExceptionAdviceConfigurer<CustomErrorReasonDTO> configurer = new ExceptionAdviceConfigurer<>(customFailureResponseWriter); // Apply response writer when exception occurs
62+
configurer.addConstraintViolation(CustomErrorCode.ERROR.getReason()); // apply with default handler
63+
configurer.addMethodArgumentNotValid(CustomErrorCode.ERROR.getReason());
64+
configurer.addServerApplication(customServerApplicationExceptionHandler, CustomErrorCode.ERROR.getReason()); // apply custom handler
65+
configurer.addGlobalException(CustomErrorCode.BAD_ERROR.getReason());
66+
return configurer;
67+
}
68+
69+
}
70+
```

api-payload/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>org.namul</groupId>
88
<artifactId>beginner</artifactId>
9-
<version>0.2.1</version>
9+
<version>0.3.0</version>
1010
</parent>
1111

1212
<artifactId>api-payload</artifactId>
Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
package org.namul.api.payload.config;
22

3-
import jakarta.validation.ConstraintViolationException;
4-
import org.namul.api.payload.error.*;
3+
import org.namul.api.payload.code.dto.ErrorReasonDTO;
4+
import org.namul.api.payload.error.ExceptionAdvice;
55
import org.namul.api.payload.error.configurer.ExceptionAdviceConfigurer;
6-
import org.namul.api.payload.error.exception.ServerApplicationException;
7-
import org.namul.api.payload.handler.*;
8-
import org.namul.api.payload.registry.ExceptionHandlerRegistry;
96
import org.namul.api.payload.writer.FailureResponseWriter;
107
import org.namul.api.payload.writer.supports.DefaultFailureResponseWriter;
118
import org.springframework.boot.autoconfigure.AutoConfiguration;
129
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
13-
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
1410
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1511
import org.springframework.context.annotation.Bean;
16-
import org.springframework.web.bind.MethodArgumentNotValidException;
17-
18-
import java.util.Optional;
1912

2013
@AutoConfiguration
2114
public class ExceptionAdviceAutoConfiguration {
@@ -26,65 +19,9 @@ DefaultFailureResponseWriter defaultFailureResponseWriter() {
2619
return new DefaultFailureResponseWriter();
2720
}
2821

29-
@Bean
30-
@ConditionalOnClass(ConstraintViolationException.class)
31-
ConstraintViolationExceptionHandler constraintViolationExceptionHandler(FailureResponseWriter failureResponseWriter) {
32-
return new ConstraintViolationExceptionHandler(failureResponseWriter);
33-
}
34-
35-
@Bean
36-
@ConditionalOnClass(MethodArgumentNotValidException.class)
37-
MethodArgumentNotValidExceptionHandler methodArgumentNotValidExceptionHandler(FailureResponseWriter failureResponseWriter) {
38-
return new MethodArgumentNotValidExceptionHandler(failureResponseWriter);
39-
}
40-
41-
@Bean
42-
ServerApplicationExceptionHandler serverApplicationExceptionHandler(FailureResponseWriter failureResponseWriter) {
43-
return new ServerApplicationExceptionHandler(failureResponseWriter);
44-
}
45-
46-
@Bean
47-
GlobalExceptionHandler globalExceptionHandler(FailureResponseWriter failureResponseWriter) {
48-
return new GlobalExceptionHandler(failureResponseWriter);
49-
}
50-
51-
@Bean
52-
@ConditionalOnMissingBean(ExceptionHandlerRegistry.class)
53-
ExceptionHandlerRegistry exceptionHandlerRegistry(Optional<ConstraintViolationExceptionHandler> constraintViolationExceptionHandler,
54-
Optional<MethodArgumentNotValidExceptionHandler> methodArgumentNotValidExceptionHandler,
55-
Optional<ServerApplicationExceptionHandler> serverApplicationExceptionHandler,
56-
GlobalExceptionHandler globalExceptionHandler) {
57-
ExceptionHandlerRegistry exceptionHandlerRegistry = new ExceptionHandlerRegistry();
58-
constraintViolationExceptionHandler.ifPresent(handler -> exceptionHandlerRegistry.addHandler(ConstraintViolationException.class, handler));
59-
methodArgumentNotValidExceptionHandler.ifPresent(handler -> exceptionHandlerRegistry.addHandler(MethodArgumentNotValidException.class, handler));
60-
serverApplicationExceptionHandler.ifPresent(handler -> exceptionHandlerRegistry.addHandler(ServerApplicationException.class, handler));
61-
exceptionHandlerRegistry.addHandler(Exception.class, globalExceptionHandler);
62-
return exceptionHandlerRegistry;
63-
}
64-
65-
@Bean
66-
@ConditionalOnBean(ExceptionAdviceConfigurer.class)
67-
@ConditionalOnClass(ConstraintViolationException.class)
68-
ConstraintViolationExceptionAdvice constraintViolationExceptionAdvice(ExceptionAdviceConfigurer configurer) {
69-
return configurer.getConstraintViolationExceptionAdvice();
70-
}
71-
72-
@Bean
73-
@ConditionalOnBean(ExceptionAdviceConfigurer.class)
74-
@ConditionalOnClass(MethodArgumentNotValidException.class)
75-
MethodArgumentNotValidExceptionAdvice methodArgumentNotValidExceptionAdvice(ExceptionAdviceConfigurer configurer) {
76-
return configurer.getMethodArgumentNotValidExceptionAdvice();
77-
}
78-
79-
@Bean
80-
@ConditionalOnBean(ExceptionAdviceConfigurer.class)
81-
ServerApplicationExceptionAdvice serverApplicationExceptionAdvice(ExceptionAdviceConfigurer configurer) {
82-
return configurer.getServerApplicationExceptionAdvice();
83-
}
84-
8522
@Bean
8623
@ConditionalOnBean(ExceptionAdviceConfigurer.class)
87-
GlobalExceptionAdvice advices(ExceptionAdviceConfigurer configurer) {
88-
return configurer.getGlobalExceptionAdvice();
24+
<R extends ErrorReasonDTO> ExceptionAdvice<R> exceptionAdvice(ExceptionAdviceConfigurer<R> exceptionAdviceConfigurer) {
25+
return new ExceptionAdvice<>(exceptionAdviceConfigurer);
8926
}
9027
}

api-payload/src/main/java/org/namul/api/payload/error/ConstraintViolationExceptionAdvice.java

Lines changed: 0 additions & 30 deletions
This file was deleted.

api-payload/src/main/java/org/namul/api/payload/error/ExceptionAdvice.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,47 @@
22

33
import jakarta.servlet.http.HttpServletRequest;
44
import jakarta.servlet.http.HttpServletResponse;
5+
import lombok.RequiredArgsConstructor;
6+
import org.namul.api.payload.code.dto.ErrorReasonDTO;
7+
import org.namul.api.payload.error.configurer.ExceptionAdviceConfigurer;
8+
import org.namul.api.payload.handler.ExceptionAdviceHandler;
9+
import org.namul.api.payload.response.BaseResponse;
10+
import org.springframework.web.bind.annotation.ExceptionHandler;
11+
import org.springframework.web.bind.annotation.RestControllerAdvice;
512

613
/**
714
* The class for Error handling when error occurs.
8-
* @param <R> The return type after handling exception
9-
* @param <T> The exception type which will be handled
1015
*/
11-
public interface ExceptionAdvice<R, T extends Exception> {
16+
@RestControllerAdvice
17+
@RequiredArgsConstructor
18+
public class ExceptionAdvice<R extends ErrorReasonDTO> {
19+
20+
private final ExceptionAdviceConfigurer<R> exceptionAdviceConfigurer;
1221

1322
/**
1423
* Handle method when exception occurs
1524
* @param e The Exception type
1625
* @return The Response value after handling exception
1726
*/
18-
R handle(T e, HttpServletRequest request, HttpServletResponse response);
27+
@ExceptionHandler
28+
public <E extends Exception> BaseResponse handle(E e, HttpServletRequest request, HttpServletResponse response) {
29+
ExceptionAdviceRegistry<E, R> registry = this.exceptionAdviceConfigurer.findRegistry(e.getClass());
30+
if (registry == null) {
31+
throw new IllegalArgumentException("The appropriate handler was not found.");
32+
}
33+
return handleDelegated(e, request, response, registry);
34+
}
35+
36+
/**
37+
* Create BaseResponse with handler and errorReasonDTO
38+
* @param e The exception class
39+
* @param request The HttpServletRequest
40+
* @param response The HttpServletResponse
41+
* @param registry The registry contains handler and ErrorReasonDTO
42+
* @return The created response
43+
* @param <E> The exception type
44+
*/
45+
private <E extends Exception> BaseResponse handleDelegated(E e, HttpServletRequest request, HttpServletResponse response, ExceptionAdviceRegistry<E, R> registry) {
46+
return registry.getHandler().handleException(e, request, response, registry.getErrorReasonDTO());
47+
}
1948
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.namul.api.payload.error;
2+
3+
import lombok.Getter;
4+
import lombok.RequiredArgsConstructor;
5+
import org.namul.api.payload.code.dto.ErrorReasonDTO;
6+
import org.namul.api.payload.handler.ExceptionAdviceHandler;
7+
8+
/**
9+
* The registry contains handler and ErrorReasonDTO
10+
* @param <E> The type of exception will be handled by handler
11+
* @param <R> The type of ErrorReasonDTO
12+
*/
13+
@Getter
14+
@RequiredArgsConstructor
15+
public class ExceptionAdviceRegistry<E extends Exception, R extends ErrorReasonDTO> {
16+
17+
private final ExceptionAdviceHandler<E, R> handler;
18+
private final R errorReasonDTO;
19+
}

api-payload/src/main/java/org/namul/api/payload/error/GlobalExceptionAdvice.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

api-payload/src/main/java/org/namul/api/payload/error/MethodArgumentNotValidExceptionAdvice.java

Lines changed: 0 additions & 38 deletions
This file was deleted.

api-payload/src/main/java/org/namul/api/payload/error/ServerApplicationExceptionAdvice.java

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.namul.api.payload.error.configurer;
2+
3+
import org.namul.api.payload.code.DefaultResponseErrorCode;
4+
import org.namul.api.payload.code.dto.supports.DefaultResponseErrorReasonDTO;
5+
import org.namul.api.payload.writer.FailureResponseWriter;
6+
7+
public class DefaultExceptionAdviceConfigurer extends ExceptionAdviceConfigurer<DefaultResponseErrorReasonDTO> {
8+
9+
public DefaultExceptionAdviceConfigurer(FailureResponseWriter<DefaultResponseErrorReasonDTO> failureResponseWriter) {
10+
super(failureResponseWriter);
11+
super.withDefault(
12+
DefaultResponseErrorCode._BAD_REQUEST.getReason(),
13+
DefaultResponseErrorCode._BAD_REQUEST.getReason(),
14+
DefaultResponseErrorCode._BAD_REQUEST.getReason(),
15+
DefaultResponseErrorCode._INTERNAL_SERVER_ERROR.getReason()
16+
);
17+
}
18+
}

0 commit comments

Comments
 (0)