Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
dinky
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangpeng
dinky
Commits
0116c112
Unverified
Commit
0116c112
authored
Dec 19, 2023
by
gaoyan
Committed by
GitHub
Dec 19, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modified the logic of the error pop-up window (#2690)
* Modified the logic of the error pop-up window * Modified the logic of the error pop-up window
parent
65bbe7bf
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
127 additions
and
41 deletions
+127
-41
UnKnownExceptionHandler.java
...java/org/dinky/aop/exception/UnKnownExceptionHandler.java
+39
-0
WebExceptionHandler.java
...ain/java/org/dinky/aop/exception/WebExceptionHandler.java
+9
-17
Result.java
dinky-admin/src/main/java/org/dinky/data/result/Result.java
+38
-7
UserServiceImpl.java
...src/main/java/org/dinky/service/impl/UserServiceImpl.java
+3
-3
CodeEnum.java
...y-common/src/main/java/org/dinky/data/enums/CodeEnum.java
+2
-1
response.ts
dinky-web/src/locales/en-US/response.ts
+2
-2
response.ts
dinky-web/src/locales/zh-CN/response.ts
+2
-2
requestErrorConfig.ts
dinky-web/src/requestErrorConfig.ts
+32
-9
No files found.
dinky-admin/src/main/java/org/dinky/aop/exception/UnKnownExceptionHandler.java
0 → 100644
View file @
0116c112
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org
.
dinky
.
aop
.
exception
;
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
lombok.extern.slf4j.Slf4j
;
@Slf4j
@ControllerAdvice
@Order
public
class
UnKnownExceptionHandler
{
@ExceptionHandler
public
Result
<
Exception
>
unknownException
(
Exception
e
)
{
return
Result
.
exception
(
e
.
getMessage
(),
e
);
}
}
dinky-admin/src/main/java/org/dinky/aop/WebExceptionHandler.java
→
dinky-admin/src/main/java/org/dinky/aop/
exception/
WebExceptionHandler.java
View file @
0116c112
...
...
@@ -17,9 +17,8 @@
*
*/
package
org
.
dinky
.
aop
;
package
org
.
dinky
.
aop
.
exception
;
import
org.dinky.data.enums.CodeEnum
;
import
org.dinky.data.enums.Status
;
import
org.dinky.data.exception.BusException
;
import
org.dinky.data.result.Result
;
...
...
@@ -32,8 +31,8 @@ import java.util.Map;
import
javax.servlet.http.HttpServletResponse
;
import
org.s
lf4j.Logger
;
import
org.s
lf4j.LoggerFactory
;
import
org.s
pringframework.core.Ordered
;
import
org.s
pringframework.core.annotation.Order
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.validation.FieldError
;
...
...
@@ -55,10 +54,9 @@ import cn.hutool.core.util.StrUtil;
* @since 2022/2/2 22:22
*/
@RestControllerAdvice
@Order
(
Ordered
.
HIGHEST_PRECEDENCE
)
public
class
WebExceptionHandler
{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
WebExceptionHandler
.
class
);
@ExceptionHandler
public
Result
<
Void
>
busException
(
BusException
e
)
{
if
(
StrUtil
.
isEmpty
(
e
.
getMsg
()))
{
...
...
@@ -83,12 +81,12 @@ public class WebExceptionHandler {
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
HttpServletResponse
response
=
servletRequestAttributes
.
getResponse
();
if
(
response
!=
null
)
{
response
.
setStatus
(
CodeEnum
.
NOTLOGIN
.
getCod
e
());
response
.
setStatus
(
HttpStatus
.
UNAUTHORIZED
.
valu
e
());
}
String
type
=
notLoginException
.
getType
();
Status
status
=
ERR_CODE_MAPPING
.
getOrDefault
(
type
,
Status
.
NOT_TOKEN
);
return
Result
.
f
ailed
(
status
);
return
Result
.
authorizeF
ailed
(
status
);
}
/**
...
...
@@ -108,19 +106,13 @@ public class WebExceptionHandler {
// 这里列出了全部错误参数,按正常逻辑,只需要第一条错误即可
FieldError
fieldError
=
(
FieldError
)
errors
.
get
(
0
);
if
(
StringUtils
.
isNotBlank
(
fieldError
.
getDefaultMessage
()))
{
return
Result
.
failed
(
return
Result
.
paramsError
(
Status
.
GLOBAL_PARAMS_CHECK_ERROR
,
fieldError
.
getField
(),
fieldError
.
getDefaultMessage
());
}
return
Result
.
failed
(
return
Result
.
paramsError
(
Status
.
GLOBAL_PARAMS_CHECK_ERROR_VALUE
,
fieldError
.
getField
(),
fieldError
.
getRejectedValue
());
}
}
return
Result
.
failed
(
Status
.
REQUEST_PARAMS_ERROR
);
}
@ExceptionHandler
public
Result
<
Void
>
unknownException
(
Exception
e
)
{
logger
.
error
(
"ERROR:"
,
e
);
return
Result
.
failed
(
Status
.
UNKNOWN_ERROR
,
(
Object
)
e
.
getMessage
());
return
Result
.
paramsError
(
Status
.
REQUEST_PARAMS_ERROR
);
}
}
dinky-admin/src/main/java/org/dinky/data/result/Result.java
View file @
0116c112
...
...
@@ -24,6 +24,7 @@ import org.dinky.data.enums.Status;
import
java.io.Serializable
;
import
java.text.MessageFormat
;
import
java.util.Objects
;
import
cn.hutool.core.date.DateTime
;
import
io.swagger.annotations.ApiModel
;
...
...
@@ -43,7 +44,9 @@ import lombok.NoArgsConstructor;
@ApiModel
(
value
=
"Result"
,
description
=
"Return Result"
)
public
class
Result
<
T
>
implements
Serializable
{
/** result data */
/**
* result data
*/
@ApiModelProperty
(
value
=
"Result Data"
,
name
=
"data"
,
...
...
@@ -59,7 +62,7 @@ public class Result<T> implements Serializable {
dataType
=
"Integer"
,
required
=
true
,
example
=
"0"
,
notes
=
"
0: success, 1: fail
"
)
notes
=
"
CodeEnum
"
)
private
Integer
code
;
@ApiModelProperty
(
...
...
@@ -70,7 +73,10 @@ public class Result<T> implements Serializable {
example
=
"success"
,
notes
=
"success: success, fail: fail"
)
private
String
msg
;
/** result time */
/**
* result time
*/
@ApiModelProperty
(
value
=
"Result Time"
,
name
=
"time"
,
...
...
@@ -79,7 +85,10 @@ public class Result<T> implements Serializable {
example
=
"2021-05-03 19:56:00"
,
notes
=
"yyyy-MM-dd HH:mm:ss"
)
private
String
time
;
/** result success */
/**
* result success
*/
@ApiModelProperty
(
value
=
"Result is Success"
,
name
=
"success"
,
...
...
@@ -148,11 +157,17 @@ public class Result<T> implements Serializable {
}
public
static
<
T
>
Result
<
T
>
of
(
T
data
,
Integer
code
,
String
msg
)
{
return
new
Result
<>(
data
,
code
,
msg
,
new
DateTime
().
toString
(),
code
==
0
);
return
new
Result
<>(
data
,
code
,
msg
,
new
DateTime
().
toString
(),
Objects
.
equals
(
code
,
CodeEnum
.
SUCCESS
.
getCode
()));
}
public
static
<
T
>
Result
<
T
>
of
(
T
data
,
Integer
code
,
Status
status
)
{
return
new
Result
<>(
data
,
code
,
status
.
getMessage
(),
new
DateTime
().
toString
(),
code
==
0
);
return
new
Result
<>(
data
,
code
,
status
.
getMessage
(),
new
DateTime
().
toString
(),
Objects
.
equals
(
code
,
CodeEnum
.
SUCCESS
.
getCode
()));
}
public
static
<
T
>
Result
<
T
>
failed
()
{
...
...
@@ -183,11 +198,27 @@ public class Result<T> implements Serializable {
return
of
(
model
,
CodeEnum
.
ERROR
.
getCode
(),
status
.
getMessage
());
}
public
static
<
T
>
Result
<
T
>
authorizeFailed
(
Status
status
)
{
return
of
(
null
,
CodeEnum
.
AUTHORIZE_ERROR
.
getCode
(),
status
.
getMessage
());
}
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
<
T
>
Result
<
T
>
paramsError
(
Status
status
,
Object
...
args
)
{
return
of
(
null
,
CodeEnum
.
PARAMS_ERROR
.
getCode
(),
MessageFormat
.
format
(
status
.
getMessage
(),
args
));
}
/**
* Call this function if there is any error
*
* @param status status
* @param args args
* @param args
args
* @return result
*/
public
static
<
T
>
Result
<
T
>
errorWithArgs
(
Status
status
,
Object
...
args
)
{
...
...
dinky-admin/src/main/java/org/dinky/service/impl/UserServiceImpl.java
View file @
0116c112
...
...
@@ -177,19 +177,19 @@ public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implemen
user
=
loginDTO
.
isLdapLogin
()
?
ldapLogin
(
loginDTO
)
:
localLogin
(
loginDTO
);
}
catch
(
AuthException
e
)
{
// Handle authentication exceptions and return the corresponding error status
return
Result
.
f
ailed
(
e
.
getStatus
()
+
e
.
getMessage
());
return
Result
.
authorizeF
ailed
(
e
.
getStatus
()
+
e
.
getMessage
());
}
// Check if the user is enabled
if
(!
user
.
getEnabled
())
{
loginLogService
.
saveLoginLog
(
user
,
Status
.
USER_DISABLED_BY_ADMIN
);
return
Result
.
f
ailed
(
Status
.
USER_DISABLED_BY_ADMIN
);
return
Result
.
authorizeF
ailed
(
Status
.
USER_DISABLED_BY_ADMIN
);
}
UserDTO
userInfo
=
refreshUserInfo
(
user
);
if
(
Asserts
.
isNullCollection
(
userInfo
.
getTenantList
()))
{
loginLogService
.
saveLoginLog
(
user
,
Status
.
USER_NOT_BINDING_TENANT
);
return
Result
.
f
ailed
(
Status
.
USER_NOT_BINDING_TENANT
);
return
Result
.
authorizeF
ailed
(
Status
.
USER_NOT_BINDING_TENANT
);
}
// Perform login using StpUtil (Assuming it handles the session management)
...
...
dinky-common/src/main/java/org/dinky/data/enums/CodeEnum.java
View file @
0116c112
...
...
@@ -29,7 +29,8 @@ public enum CodeEnum {
ERROR
(
1
),
EXCEPTION
(
5
),
NOTLOGIN
(
401
);
PARAMS_ERROR
(
6
),
AUTHORIZE_ERROR
(
7
);
private
Integer
code
;
...
...
dinky-web/src/locales/en-US/response.ts
View file @
0116c112
...
...
@@ -20,8 +20,8 @@
export
default
{
'app.response.response-error'
:
'ResponseError'
,
'app.response.sucess'
:
'Success'
,
'app.response.error'
:
'
Error
'
,
'app.response.error'
:
'
The operation was unsuccessful
'
,
'app.response.exception'
:
'Server exception'
,
'app.response.notlogin'
:
'
Not Login
'
,
'app.response.notlogin'
:
'
Authentication failed
'
,
'app.response.notexist'
:
'you visit the page or interface does not exist'
};
dinky-web/src/locales/zh-CN/response.ts
View file @
0116c112
...
...
@@ -20,8 +20,8 @@
export
default
{
'app.response.response-error'
:
'响应出错'
,
'app.response.sucess'
:
'成功'
,
'app.response.error'
:
'
错误
'
,
'app.response.error'
:
'
操作未成功
'
,
'app.response.exception'
:
'服务端错误'
,
'app.response.notlogin'
:
'
未登录
'
,
'app.response.notlogin'
:
'
身份认证失败
'
,
'app.response.notexist'
:
'你访问的页面/接口不存在'
};
dinky-web/src/requestErrorConfig.ts
View file @
0116c112
...
...
@@ -19,27 +19,50 @@
import
{
API_CONSTANTS
}
from
'@/services/endpoints'
;
import
{
l
}
from
'@/utils/intl'
;
import
{
ErrorNotification
}
from
'@/utils/messages'
;
import
{
ErrorNotification
,
WarningNotification
}
from
'@/utils/messages'
;
import
{
history
}
from
'@@/core/history'
;
import
type
{
RequestOptions
}
from
'@@/plugin-request/request'
;
import
type
{
RequestConfig
}
from
'@umijs/max'
;
// 错误处理方案: 错误类型
enum
ErrorCode
{
'app.response.sucess'
=
0
,
'app.response.error'
=
1
,
'app.response.exception'
=
5
,
'app.response.notlogin'
=
401
SUCCESS
=
0
,
ERROR
=
1
,
EXCEPTION
=
5
,
PARAMS_ERROR
=
6
,
AUTHORIZE_ERROR
=
7
}
// 与后端约定的响应数据格式
interface
ResponseStructure
{
success
:
boolean
;
data
?:
boolean
;
data
?:
any
;
code
:
number
;
msg
:
string
;
}
const
handleBizError
=
(
result
:
ResponseStructure
)
=>
{
const
{
msg
,
code
,
data
}
=
result
;
switch
(
code
)
{
case
ErrorCode
.
SUCCESS
:
//don't deal with it, just be happy
break
;
case
ErrorCode
.
ERROR
:
WarningNotification
(
msg
,
l
(
'app.response.error'
));
break
;
case
ErrorCode
.
EXCEPTION
:
//TODO 可配置化,dev换弹出错误,release不弹
//ErrorNotification(JSON.stringify(data), l('app.response.error'));
break
;
case
ErrorCode
.
PARAMS_ERROR
:
ErrorNotification
(
msg
,
l
(
'app.response.error'
));
break
;
case
ErrorCode
.
AUTHORIZE_ERROR
:
ErrorNotification
(
msg
,
l
(
'app.response.notlogin'
));
break
;
}
};
/**
* @name 错误处理
* pro 自带的错误处理, 可以在这里做自己的改动
...
...
@@ -65,8 +88,7 @@ export const errorConfig: RequestConfig = {
if
(
error
.
name
===
'BizError'
)
{
const
errorInfo
:
ResponseStructure
=
error
.
info
;
if
(
errorInfo
)
{
const
{
msg
,
code
}
=
errorInfo
;
ErrorNotification
(
msg
,
l
(
ErrorCode
[
code
],
'Error'
));
handleBizError
(
errorInfo
);
}
}
else
if
(
error
.
response
)
{
// 请求成功发出且服务器也响应了状态码,但状态代码超出了 2xx 的范围
...
...
@@ -75,7 +97,8 @@ export const errorConfig: RequestConfig = {
history
.
push
(
API_CONSTANTS
.
LOGIN_PATH
);
}
else
{
//预留,处理其他code逻辑,目前未定义的code统一发送错误通知
ErrorNotification
(
error
.
message
,
error
.
code
);
//TODO 可配置化,dev换弹出错误,release不弹
// ErrorNotification(error.message, error.code);
}
}
else
if
(
error
.
request
)
{
// 请求已经成功发起,但没有收到响应
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment