Unverified Commit 1f496aef authored by gaoyan's avatar gaoyan Committed by GitHub

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
parent 8b6c469d
......@@ -24,6 +24,7 @@ import org.dinky.data.result.Result;
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 @@ import lombok.extern.slf4j.Slf4j;
public class UnKnownExceptionHandler {
@ExceptionHandler
public Result<Exception> unknownException(Exception e) {
@ResponseBody
public Result<String> unknownException(Exception e) {
log.error(e.getMessage(), e);
return Result.exception(e.getMessage(), e);
}
......
......@@ -39,6 +39,7 @@ import org.springframework.validation.FieldError;
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 @@ import cn.hutool.core.util.StrUtil;
public class WebExceptionHandler {
@ExceptionHandler
@ResponseBody
public Result<Void> busException(BusException e) {
if (StrUtil.isEmpty(e.getMsg())) {
return Result.failed(I18n.getMessage(e.getCode(), e.getMessage()));
......@@ -76,6 +78,7 @@ public class WebExceptionHandler {
.build();
@ExceptionHandler
@ResponseBody
public Result<Void> notLoginException(NotLoginException notLoginException) {
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
......@@ -97,6 +100,7 @@ public class WebExceptionHandler {
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseBody
public Result<String> paramExceptionHandler(MethodArgumentNotValidException e) {
BindingResult exceptions = e.getBindingResult();
// 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
......
......@@ -21,6 +21,7 @@ package org.dinky.data.result;
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 class Result<T> implements Serializable {
return of(null, CodeEnum.AUTHORIZE_ERROR.getCode(), msg);
}
public static Result<Exception> exception(String msg, Exception e) {
return of(e, CodeEnum.EXCEPTION.getCode(), msg);
public static Result<String> exception(String msg, Exception e) {
return of(LogUtil.getError(e), CodeEnum.EXCEPTION.getCode(), msg);
}
public static <T> Result<T> paramsError(Status status, Object... args) {
......
......@@ -44,6 +44,6 @@ export const initSomeThing = () => {
setLocalThemeToStorage();
// 取出本地存储是否有启用消息提示的 key , 没有的话设置一下
if (hasKeyofLocalStorage(ENABLE_MODEL_TIP)) {
setKeyToLocalStorage(ENABLE_MODEL_TIP, 'true');
setKeyToLocalStorage(ENABLE_MODEL_TIP, 'false');
}
};
......@@ -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) {
// 请求已经成功发起,但没有收到响应
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment