From 1f496aefad92ed8e6376cf03c0c0fc827a003e2e Mon Sep 17 00:00:00 2001 From: gaoyan Date: Wed, 20 Dec 2023 13:00:30 +0800 Subject: [PATCH] Fix exception (#2696) * fix log error * Fixed the issue that Exception was not caught and returned * Fixed the issue that Exception was not caught and returned * formate cod --- .../dinky/aop/exception/UnKnownExceptionHandler.java | 4 +++- .../org/dinky/aop/exception/WebExceptionHandler.java | 4 ++++ .../src/main/java/org/dinky/data/result/Result.java | 5 +++-- dinky-web/src/pages/Other/Login/function.tsx | 2 +- dinky-web/src/requestErrorConfig.ts | 11 ++++------- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/dinky-admin/src/main/java/org/dinky/aop/exception/UnKnownExceptionHandler.java b/dinky-admin/src/main/java/org/dinky/aop/exception/UnKnownExceptionHandler.java index 37038aa9a5..de4c4a2047 100644 --- a/dinky-admin/src/main/java/org/dinky/aop/exception/UnKnownExceptionHandler.java +++ b/dinky-admin/src/main/java/org/dinky/aop/exception/UnKnownExceptionHandler.java @@ -24,6 +24,7 @@ import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; import lombok.extern.slf4j.Slf4j; @@ -33,7 +34,8 @@ public class UnKnownExceptionHandler { @ExceptionHandler - public Result unknownException(Exception e) { + @ResponseBody + public Result unknownException(Exception e) { log.error(e.getMessage(), e); return Result.exception(e.getMessage(), e); } diff --git a/dinky-admin/src/main/java/org/dinky/aop/exception/WebExceptionHandler.java b/dinky-admin/src/main/java/org/dinky/aop/exception/WebExceptionHandler.java index 252401ef2b..6e87079141 100644 --- a/dinky-admin/src/main/java/org/dinky/aop/exception/WebExceptionHandler.java +++ b/dinky-admin/src/main/java/org/dinky/aop/exception/WebExceptionHandler.java @@ -39,6 +39,7 @@ import org.springframework.validation.ObjectError; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.context.request.RequestContextHolder; @@ -58,6 +59,7 @@ public class WebExceptionHandler { @ExceptionHandler + @ResponseBody public Result busException(BusException e) { if (StrUtil.isEmpty(e.getMsg())) { return Result.failed(I18n.getMessage(e.getCode(), e.getMessage())); @@ -76,6 +78,7 @@ public Result busException(BusException e) { .build(); @ExceptionHandler + @ResponseBody public Result notLoginException(NotLoginException notLoginException) { ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); @@ -97,6 +100,7 @@ public Result notLoginException(NotLoginException notLoginException) { */ @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler({MethodArgumentNotValidException.class}) + @ResponseBody public Result paramExceptionHandler(MethodArgumentNotValidException e) { BindingResult exceptions = e.getBindingResult(); // 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息 diff --git a/dinky-admin/src/main/java/org/dinky/data/result/Result.java b/dinky-admin/src/main/java/org/dinky/data/result/Result.java index 5dc6178581..97b6239874 100644 --- a/dinky-admin/src/main/java/org/dinky/data/result/Result.java +++ b/dinky-admin/src/main/java/org/dinky/data/result/Result.java @@ -21,6 +21,7 @@ import org.dinky.data.enums.CodeEnum; import org.dinky.data.enums.Status; +import org.dinky.utils.LogUtil; import java.io.Serializable; import java.text.MessageFormat; @@ -206,8 +207,8 @@ public static Result authorizeFailed(String msg) { return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), msg); } - public static Result exception(String msg, Exception e) { - return of(e, CodeEnum.EXCEPTION.getCode(), msg); + public static Result exception(String msg, Exception e) { + return of(LogUtil.getError(e), CodeEnum.EXCEPTION.getCode(), msg); } public static Result paramsError(Status status, Object... args) { diff --git a/dinky-web/src/pages/Other/Login/function.tsx b/dinky-web/src/pages/Other/Login/function.tsx index 027478c41f..a14180b890 100644 --- a/dinky-web/src/pages/Other/Login/function.tsx +++ b/dinky-web/src/pages/Other/Login/function.tsx @@ -44,6 +44,6 @@ export const initSomeThing = () => { setLocalThemeToStorage(); // 取出本地存储是否有启用消息提示的 key , 没有的话设置一下 if (hasKeyofLocalStorage(ENABLE_MODEL_TIP)) { - setKeyToLocalStorage(ENABLE_MODEL_TIP, 'true'); + setKeyToLocalStorage(ENABLE_MODEL_TIP, 'false'); } }; diff --git a/dinky-web/src/requestErrorConfig.ts b/dinky-web/src/requestErrorConfig.ts index 4bc3520ae6..a413c60d29 100644 --- a/dinky-web/src/requestErrorConfig.ts +++ b/dinky-web/src/requestErrorConfig.ts @@ -45,7 +45,6 @@ interface ResponseStructure { const handleBizError = (result: ResponseStructure) => { const { msg, code, data } = result; - console.log(JSON.stringify(msg)); switch (code) { case ErrorCode.SUCCESS: @@ -55,9 +54,8 @@ const handleBizError = (result: ResponseStructure) => { WarningNotification(msg, l('app.response.error')); break; case ErrorCode.EXCEPTION: - //TODO 可配置化,dev换弹出错误,release不弹 if (Boolean(getValueFromLocalStorage(ENABLE_MODEL_TIP))) { - ErrorNotification(JSON.stringify(data), l('app.response.error')); + ErrorNotification(data, l('app.response.exception')); } break; case ErrorCode.PARAMS_ERROR: @@ -89,7 +87,6 @@ export const errorConfig: RequestConfig = { }, // 错误接收及处理 errorHandler: (error: any, opts: any) => { - console.log(error); if (opts?.skipErrorHandler) throw error; // 我们的 errorThrower 抛出的错误。 @@ -104,9 +101,9 @@ export const errorConfig: RequestConfig = { if (error.response.status === 401) { history.push(API_CONSTANTS.LOGIN_PATH); } else { - //预留,处理其他code逻辑,目前未定义的code统一发送错误通知 - //TODO 可配置化,dev换弹出错误,release不弹 - // ErrorNotification(error.message, error.code); + if (Boolean(getValueFromLocalStorage(ENABLE_MODEL_TIP))) { + ErrorNotification(error.message, error.code); + } } } else if (error.request) { // 请求已经成功发起,但没有收到响应