From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/BladeRestExceptionTranslator.java | 83 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 83 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/BladeRestExceptionTranslator.java b/Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/BladeRestExceptionTranslator.java new file mode 100644 index 0000000..f475928 --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/BladeRestExceptionTranslator.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 搴勯獮 (smallchill@163.com) + */ +package org.springblade.core.log.error; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.log.exception.ServiceException; +import org.springblade.core.log.props.BladeRequestLogProperties; +import org.springblade.core.log.publisher.ErrorLogPublisher; +import org.springblade.core.secure.exception.SecureException; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.api.ResultCode; +import org.springblade.core.tool.utils.Func; +import org.springblade.core.tool.utils.UrlUtil; +import org.springblade.core.tool.utils.WebUtil; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.DispatcherServlet; + +import javax.servlet.Servlet; + +/** + * 鏈煡寮傚父杞瘧鍜屽彂閫侊紝鏂逛究鐩戝惉锛屽鏈煡寮傚父缁熶竴澶勭悊銆侽rder 鎺掑簭浼樺厛绾т綆 + * + * @author Chill + */ +@Slf4j +@Order +@RequiredArgsConstructor +@AutoConfiguration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) +@RestControllerAdvice +public class BladeRestExceptionTranslator { + + private final BladeRequestLogProperties properties; + + @ExceptionHandler(ServiceException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(ServiceException e) { + log.error("涓氬姟寮傚父", e); + return R.fail(e.getResultCode(), e.getMessage()); + } + + @ExceptionHandler(SecureException.class) + @ResponseStatus(HttpStatus.UNAUTHORIZED) + public R handleError(SecureException e) { + log.error("璁よ瘉寮傚父", e); + return R.fail(e.getResultCode(), e.getMessage()); + } + + @ExceptionHandler(Throwable.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public R handleError(Throwable e) { + log.error("鏈嶅姟鍣ㄥ紓甯�", e); + if (properties.getErrorLog()) { + //鍙戦�佹湇鍔″紓甯镐簨浠� + ErrorLogPublisher.publishEvent(e, UrlUtil.getPath(WebUtil.getRequest().getRequestURI())); + } + return R.fail(ResultCode.INTERNAL_SERVER_ERROR, (Func.isEmpty(e.getMessage()) ? ResultCode.INTERNAL_SERVER_ERROR.getMessage() : e.getMessage())); + } + +} -- Gitblit v1.9.3