将 AllExceptionHandler 提取到 plusone-exception-handler 中。

This commit is contained in:
zhouxy108 2022-12-11 02:37:13 +08:00
parent 509e195fee
commit 0ab296358d
3 changed files with 21 additions and 30 deletions

View File

@ -1,29 +0,0 @@
package xyz.zhouxy.plusone.exception.handler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import xyz.zhouxy.plusone.util.RestfulResult;
/**
* 处理所有异常的处理器
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*/
@ConditionalOnProperty(prefix = "plusone.exception", name = "handle-all-exception", havingValue = "true")
@RestControllerAdvice
@Slf4j
public class AllExceptionHandler extends BaseExceptionHandler {
protected AllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
super(exceptionInfoHolder);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<RestfulResult> handleException(Throwable e) {
log.error(e.getMessage(), e);
return this.buildExceptionResponse(e);
}
}

View File

@ -0,0 +1,20 @@
package xyz.zhouxy.plusone.exception.handler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import xyz.zhouxy.plusone.exception.handler.BaseExceptionHandler.ExceptionInfoHolder;
/**
* AllExceptionHandlerConfig
*/
@Configuration
@ConditionalOnProperty(prefix = "plusone.exception", name = "handle-all-exception", havingValue = "true")
public class AllExceptionHandlerConfig {
@Bean
AllExceptionHandler getAllExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
return new AllExceptionHandler(exceptionInfoHolder);
}
}

View File

@ -39,7 +39,7 @@
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-exception-handler</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>
</dependency>
</dependencies>