From be97d359d0bd461909d03ed2820bde6f29251517 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Thu, 30 Nov 2023 20:46:56 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=96=B0=E5=A2=9E=E5=85=A8?= =?UTF-8?q?=E5=B1=80=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86=E5=99=A8=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GlobalErrorHandlerAutoConfiguration.java | 39 ++++++++ .../crud/handler/GlobalErrorHandler.java | 89 +++++++++++++++++++ ...ot.autoconfigure.AutoConfiguration.imports | 3 +- 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalErrorHandlerAutoConfiguration.java create mode 100644 continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalErrorHandlerAutoConfiguration.java b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalErrorHandlerAutoConfiguration.java new file mode 100644 index 00000000..9bc8f9a9 --- /dev/null +++ b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/autoconfigure/GlobalErrorHandlerAutoConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.extension.crud.autoconfigure; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Import; +import top.charles7c.continew.starter.extension.crud.handler.GlobalErrorHandler; + +/** + * 全局错误处理器自动配置 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@AutoConfiguration +@Import(GlobalErrorHandler.class) +@ConditionalOnMissingBean(BasicErrorController.class) +@ComponentScan("top.charles7c.continew.starter.extension.crud.handler") +public class GlobalErrorHandlerAutoConfiguration { +} diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java new file mode 100644 index 00000000..f777ddb1 --- /dev/null +++ b/continew-starter-extension/continew-starter-extension-crud/src/main/java/top/charles7c/continew/starter/extension/crud/handler/GlobalErrorHandler.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.charles7c.continew.starter.extension.crud.handler; + +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.json.JSONUtil; +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; +import top.charles7c.continew.starter.extension.crud.model.resp.R; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +/** + * 全局错误处理器 + * + * @author Charles7c + * @since 1.0.0 + */ +@Slf4j +@RestController +public class GlobalErrorHandler extends BasicErrorController { + + @Resource + private ObjectMapper objectMapper; + + public GlobalErrorHandler(ErrorAttributes errorAttributes, ServerProperties serverProperties, + List errorViewResolvers) { + super(errorAttributes, serverProperties.getError(), errorViewResolvers); + } + + @Override + public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) { + Map errorAttributeMap = + super.getErrorAttributes(request, super.getErrorAttributeOptions(request, MediaType.TEXT_HTML)); + String path = (String)errorAttributeMap.get("path"); + HttpStatus status = super.getStatus(request); + R result = R.fail(status.value(), (String)errorAttributeMap.get("error")); + result.setData(path); + try { + response.setStatus(HttpStatus.OK.value()); + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + objectMapper.writeValue(response.getWriter(), result); + } catch (IOException e) { + log.error("请求地址 [{}],默认错误处理时发生 IO 异常。", path, e); + } + log.error("请求地址 [{}],发生错误,错误信息:{}。", path, JSONUtil.toJsonStr(errorAttributeMap)); + return null; + } + + @Override + public ResponseEntity> error(HttpServletRequest request) { + Map errorAttributeMap = + super.getErrorAttributes(request, super.getErrorAttributeOptions(request, MediaType.ALL)); + String path = (String)errorAttributeMap.get("path"); + HttpStatus status = super.getStatus(request); + R result = R.fail(status.value(), (String)errorAttributeMap.get("error")); + result.setData(path); + log.error("请求地址 [{}],发生错误,错误信息:{}。", path, JSONUtil.toJsonStr(errorAttributeMap)); + return new ResponseEntity<>(BeanUtil.beanToMap(result), HttpStatus.OK); + } +} diff --git a/continew-starter-extension/continew-starter-extension-crud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-extension/continew-starter-extension-crud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports index c0849a43..d0ae80fc 100644 --- a/continew-starter-extension/continew-starter-extension-crud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports +++ b/continew-starter-extension/continew-starter-extension-crud/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -1 +1,2 @@ -top.charles7c.continew.starter.extension.crud.autoconfigure.CrudAutoConfiguration \ No newline at end of file +top.charles7c.continew.starter.extension.crud.autoconfigure.CrudAutoConfiguration +top.charles7c.continew.starter.extension.crud.autoconfigure.GlobalErrorHandlerAutoConfiguration \ No newline at end of file