Skip to content

Commit

Permalink
Fix exception (#2696)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gaoyan1998 authored Dec 20, 2023
1 parent 8b6c469 commit 1f496ae
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
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

0 comments on commit 1f496ae

Please sign in to comment.