Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception #2696

Merged
merged 7 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +34,8 @@
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -58,6 +59,7 @@
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()));
Expand All @@ -76,6 +78,7 @@ public Result<Void> busException(BusException e) {
.build();

@ExceptionHandler
@ResponseBody
public Result<Void> notLoginException(NotLoginException notLoginException) {
ServletRequestAttributes servletRequestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
Expand All @@ -97,6 +100,7 @@ public Result<Void> notLoginException(NotLoginException notLoginException) {
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({MethodArgumentNotValidException.class})
@ResponseBody
public Result<String> paramExceptionHandler(MethodArgumentNotValidException e) {
BindingResult exceptions = e.getBindingResult();
// 判断异常中是否有错误信息,如果存在就使用异常中的消息,否则使用默认消息
Expand Down
5 changes: 3 additions & 2 deletions dinky-admin/src/main/java/org/dinky/data/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -206,8 +207,8 @@ public static <T> Result<T> authorizeFailed(String msg) {
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) {
Expand Down
2 changes: 1 addition & 1 deletion dinky-web/src/pages/Other/Login/function.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ export const initSomeThing = () => {
setLocalThemeToStorage();
// 取出本地存储是否有启用消息提示的 key , 没有的话设置一下
if (hasKeyofLocalStorage(ENABLE_MODEL_TIP)) {
setKeyToLocalStorage(ENABLE_MODEL_TIP, 'true');
setKeyToLocalStorage(ENABLE_MODEL_TIP, 'false');
}
};
11 changes: 4 additions & 7 deletions dinky-web/src/requestErrorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -89,7 +87,6 @@ export const errorConfig: RequestConfig = {
},
// 错误接收及处理
errorHandler: (error: any, opts: any) => {
console.log(error);

if (opts?.skipErrorHandler) throw error;
// 我们的 errorThrower 抛出的错误。
Expand All @@ -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) {
// 请求已经成功发起,但没有收到响应
Expand Down
Loading